2015-02-12 56 views
2

我在3个控制器中使用rootScope变量。第一个控制器设置第二个控制器用于ng-show的值。但是当我试图通过相同的rootScope变量来控制第三个控制器的ng显示时,它不起作用。 “modstat”是变量。我是Angular的新手。所以有人请帮助我。代码如下:当在第三个控制器中使用RootScope时不工作

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

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

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

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

 
\t \t }; 
 
\t \t 
 

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

+1

并非以角的专家,应该不是函数变量是在相同的顺序排列,说明哪些对象注入? – Andreas 2015-02-12 09:16:27

+0

@Andreas非常感谢。它的固定。请发表您的评论作为您的答案。 – winnyboy5 2015-02-12 09:28:28

+0

太棒了!添加了答案:) – Andreas 2015-02-12 09:33:53

回答

1

当设置要在函数中注入的对象时,它们需要按照您在数组中的状态相同的顺序进行。你的榜样 - >

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

+0

不,它没有工作。模式从“菜单Ctrl”调用正确,但不是从Gridctrl调用 – winnyboy5 2015-02-12 09:10:17

相关问题