3

3 つのコントローラーで rootScope 変数を使用しています。最初のコントローラーは値を設定し、これは ng-show の 2 番目のコントローラーによって使用されます。しかし、同じ rootScope 変数を介して 3 番目のコントローラーから ng-show を制御しようとすると、機能しません。「modstat」 は変数です。私はAngularが初めてです。だから誰かがこれで私を助けてください。コードを以下に示します。

urlboxControllers.controller('MenuCtrl', ['$scope', '$rootScope', '$location', 'SessionService', 'AuthenticationService', 'ModalService',
	function($scope, $location, $rootScope, SessionService, AuthenticationService, ModalService){
		$scope.fname = JSON.parse(sessionStorage.fname);
		$scope.lname = JSON.parse(sessionStorage.lname);
		$scope.addLink = function() {
			$rootScope.modstat = !$rootScope.modstat;
			$rootScope.modserv = true;
			//ModalService.set(1); 
		};	
		
		
		
		$scope.logout = function() { 
		AuthenticationService.logout().success(function() {
			$location.path('/login');
		});	
			
		};

	}]);
	
urlboxControllers.controller('ModalCtrl', ['$scope',  '$rootScope', '$location', 'SessionService', 'ModalService',
function($scope, $location, $rootScope, SessionService, ModalService){
		$scope.grid = {};

		
		$scope.grid.uid = JSON.parse(sessionStorage.id);
		$scope.modserv = function() {
        		return $rootScope.modserv;
   		 };
		 
		if($scope.modserv){
			$scope.mtitle= "Create a Link";
		}
		else if(!$scope.modserv){
			$scope.mtitle= "Update Link";
		}
		
		$scope.statcheck = function() {
			console.log("stat" + $rootScope.modstat);
        		return $rootScope.modstat;
   		 };

		
		
		$scope.icons = [{ico: 'cloud'}, {ico: 'envelope'}, {ico: 'pencil'}, {ico: 'search'}, {ico: 'heart'}, {ico: 'star'},
		 {ico: 'user'}, {ico: 'home'}, {ico: 'cog'}, {ico: 'file'}, {ico: 'time'}, {ico: 'download-alt'}, {ico: 'download'},
		{ico: 'upload'}, {ico: 'lock'}, {ico: 'play-circle'}, {ico: 'tag'}, {ico: 'tags'}, {ico: 'book'}, {ico: 'bookmark'}, 
		{ico: 'print'}, {ico: 'map-marker'}, {ico: 'tint'}, {ico: 'edit'}, {ico: 'share'}, {ico: 'plus-sign'},
		 {ico: 'minus-sign'}, {ico: 'ok-sign'}, {ico: 'info-sign'}, {ico: 'leaf'}, {ico: 'fire'}, {ico: 'calendar'},
		 {ico: 'comment'}, {ico: 'folder-close'}, {ico: 'hdd'}, {ico: 'bullhorn'}, {ico: 'bell'}, {ico: 'certificate'},
		 {ico: 'wrench'}, {ico: 'globe'}, {ico: 'tasks'}, {ico: 'filter'}, {ico: 'briefcase'}, {ico: 'link'},
		 {ico: 'paperclip'}, {ico: 'pushpin'}, {ico: 'log-in'}, {ico: 'flash'}, {ico: 'log-out'}, {ico: 'save'}, {ico: 'open'},
		{ico: 'saved'}, {ico: 'send'}, {ico: 'floppy-disk'}, {ico: 'floppy-saved'}, {ico: 'tower'}, {ico: 'stats'}, 
		{ico: 'cloud-download'}, {ico: 'cloud-upload'}, {ico: 'tree-conifer'}, {ico: 'tree-deciduous'} ];
		
		
		
		$scope.create = function(){
			if($scope.modserv) {
				
					$scope.repo = ModalService.mvalidate($scope.grid);
					
					if($scope.repo.stat === 'error'){
						$scope.errcol = $scope.repo; 
					}
					else if($scope.repo.stat === 'success'){
						$scope.errcol = $scope.repo; 
						ModalService.create($scope.grid).success(function(data) {
						
							if(data='Saved')
							{
								$scope.grid = {};
								$scope.succ = '<div class="alert alert-success" role="alert">Saved</div>';
								$rootScope.modstat = false;
							}
							else {
								alert(data.error);
							}
						
						});
					}
		
			
			}

		};
		

	
		
		
		
	}]);
	
	
	urlboxControllers.controller('GridCtrl', ['$scope', '$location', 'SessionService', 'GridService', '$rootScope',
function($scope, $location, SessionService, GridService, $rootScope){
		$scope.uid = JSON.parse(sessionStorage.id);
		
		GridService.gdata($scope.uid).success(function(data) {
				$scope.grids=data;
			});
		
		$('#gridmod').on('hidden.bs.modal', function (e) {
  			GridService.gdata($scope.uid).success(function(data) {
				$scope.grids=data;		
			});
			
		});
		
		$scope.getLocation = function() {
    		if (navigator.geolocation) {
        		navigator.geolocation.getCurrentPosition($scope.showPosition);
    		} else {
        		console.log("Geolocation is not supported by this browser.");
    		}
		};
	$scope.showPosition = function (position) {
    	$scope.lat = position.coords.latitude;
        $scope.lng = position.coords.longitude;
		GridService.weather($scope.lat, $scope.lng).success(function(data) {
				$scope.wdata = data;
				$scope.weather = $scope.wdata.weather;
				$scope.main = $scope.wdata.main;
				$scope.conv = 273.15;
				$scope.temp = $scope.main.temp - $scope.conv;
				$scope.code = $scope.weather[0].icon.slice(0,-1);
			});
	};
		$scope.getLocation();
		
		$scope.upLink = function(lid) {
			//$rootScope.modstat = !$rootScope.modstat;
			console.log("test" + $rootScope.modstat);
			$rootScope.modserv = true;
		};
		$scope.delLink = function(lid) {
			console.log(lid);
		};
	
	
	}]);

4

2 に答える 2

2

問題は2番目のコントローラーに関連していると思います。

2 番目のコントローラー宣言では、$location引数と$rootScope関数の符号が反転しています。あなたはこれを使用しています:

urlboxControllers.controller('ModalCtrl', ['$scope',  '$rootScope', '$location', 'SessionService', 'ModalService',
function($scope, $location, $rootScope, SessionService, ModalService){

それ以外の:

urlboxControllers.controller('ModalCtrl', ['$scope',  '$rootScope', '$location', 'SessionService', 'ModalService',
function($scope,  $rootScope, $location, SessionService, ModalService){

2 番目のコントローラーでは、属性を$locationオブジェクトに設定し (そのため、エラーは発生しません)、次の方法で取得されます。$rootScope

于 2015-02-12T08:56:25.133 に答える
2

オブジェクトを関数に注入するように設定するときは、配列で指定したのと同じ順序にする必要があります。あなたの例 - >

urlboxControllers.controller('MenuCtrl', ['$scope', '$rootScope', '$location', 'SessionService', 'AuthenticationService', 'ModalService',
function($scope, $location, $rootScope, SessionService, AuthenticationService, ModalService){}

する必要があります

urlboxControllers.controller('MenuCtrl', ['$scope', '$rootScope', '$location', 'SessionService', 'AuthenticationService', 'ModalService',
function($scope, $rootScope, $location, SessionService, AuthenticationService, ModalService){}
于 2015-02-12T09:33:11.670 に答える