如何在AngularJS中观察$ stateProvider的状态变化?

Tim*_*sen 55 angularjs

我知道我可以跑:

scope.$watch(someItem, function(){})
Run Code Online (Sandbox Code Playgroud)

但我无法想出一种方法来监视$state.$current.name我的应用程序中的更改.

Gab*_*abe 143

它在文档中:https://github.com/angular-ui/ui-router/wiki#state-change-events

$rootScope.$on('$stateChangeStart', 
function(event, toState, toParams, fromState, fromParams){ 
    // do something
})
Run Code Online (Sandbox Code Playgroud)

  • 你需要将$ rootScope注入你调用它的控制器中. (10认同)

Crh*_*rez 26

这有效:

$scope.$watch(function(){
    return $state.$current.name
}, function(newVal, oldVal){
    //do something with values
}) 
Run Code Online (Sandbox Code Playgroud)

  • 这有助于我指导实施.谢谢! (3认同)