2015-02-12 55 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

相關問題