在第三个控制器中使用时RootScope不工作

RootScope not working when used in a third controller

本文关键字:RootScope 工作 控制器 三个      更新时间:2023-09-26

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

在函数中设置要注入的对象时,它们需要按照与数组中状态相同的顺序排列。您的示例->

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

我认为问题与第二个控制器有关。

第二个的控制器声明的$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 检索该属性