2015-11-18 113 views
3

我試圖每次更改路由時更新導航欄,我試過這個,但它似乎並沒有工作。每當視圖改變時它應該寫入bob,但它只在控制器加載時才寫入。AngularJS:查看不會更新

nav.controller.js

angular.module('app') 
.controller('navController', ['$scope', '$state', function ($scope, $state) { 
    var timeSpan; 
    $scope.user; 

    ... 

    // Update when the view changes. 
    $scope.reload = function() { 
     $state.reload(); 
    }; 
    $scope.$state = $state; 
    $scope.$watch('$state.$current.locals.globals.view', function() { 
     console.log('bob'); 
     $scope.user = userService.get(); 
    }); 

    ... 

    $scope.reroute = function(route){ 
     $state.go(route); 
    }; 
}]); 


route.js

angular.module('SimPlannerApp') 
.config(function ($stateProvider, $urlRouterProvider) { 
    $urlRouterProvider.when('/','/signin'); 

    $stateProvider 
     .state('signin', { 
      url: "/signin", 
      templateUrl: 'views/signin.html', 
      controller: 'signinController' 
     }) 
     .state('menu', { 
      url: "/menu", 
      templateUrl: 'views/menu.html', 
      controller: 'menuController', 
      resolve: { 
       config: function (configService) { 
        return configService.getConfig() 
         .then(function(response){ 
          return response.data; 
         }) 
         .catch(function(error){ 
          console.log('Error : ', error); 
          return undefined; 
         }); 
       } 
      } 
     }) 
     .state('view', { 
      url: '/view/:view', 
      templateUrl: 'views/view.html', 
      controller: 'viewController', 
      resolve: { 
       view: function ($stateParams, configService) { 
        return configService.getConfig() 
         .then(function(response){ 
          var config = response.data; 
          for (var i = 0; i < config.views.length; i++) { 
           if (config.views[i].route === $stateParams.view) { 
            return config.views[i]; 
           } 

          } 

          return undefined; 
         }) 
         .catch(function(error){ 
          console.log('Error : ', error); 
          return undefined; 
         }); 
       }, 
       config: function (configService) { 
        return configService.getConfig() 
         .then(function(response){ 
          return response.data; 
         }) 
         .catch(function(error){ 
          console.log('Error : ', error); 
          return undefined; 
         }); 
       } 
      } 
     }) 
     .state('404', { 
      url: '{path:.*}', 
      templateUrl: 'views/404.html', 
      controller: 'errorController' 
     }); 
}); 


PS:
大概應該提到我使用的是AngularJSui-router

+1

你應該發佈你的'ui-router'配置 –

+0

@AndreKreienbring:剛添加它^^ –

回答

2

你可以用$locationChangeStart處理類似如下:

$rootScope.$on('$locationChangeStart', function() { 
    console.log('bob');  
}); 

你應該寫這個在.module.run()功能。

angular 
    .module() 
    .run(function($rootScope) { 
     // to Here 
    }) 
3

不知道我是否完全理解,但使用$stateChangeSuccess來檢測路線何時更改會不會更好?我用這個在.run

$rootScope.$on('$stateChangeSuccess', 
function(event, toState, toParams, fromState, fromParams){ ... }) 

,但你可以把這個在你的控制器

https://github.com/angular-ui/ui-router/wiki