在第三个控制器中使用时,RootScope无法正常工作

时间:2015-02-12 08:36:19

标签: javascript angularjs angularjs-scope rootscope

我在3个控制器中使用rootScope变量。第一个控制器设置值,第二个控制器用于ng-show。但是当我试图通过相同的rootScope变量从第三个控制器控制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);
		};
	
	
	}]);
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分: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){

在第二个控制器中,您要在$location对象中设置一个属性(这就是为什么您没有收到任何错误),然后通过{{1 }}

答案 1 :(得分:1)

当设置要注入函数的对象时,它们需要与数组中的状态相同。你的例子 - &gt;

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){}