3

我在 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);
		};
	
	
	}]);

4

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

在第二个控制器中,您将属性设置到$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 回答