本文介绍了UI-路由器-每次路由更改时都运行功能-状态名称在何处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用Angularjs和UI-Router,尝试在每次状态更改时运行一个函数
Using Angularjs and UI-Router, trying to run a function every time state changes
$rootScope.$on('$stateChangeStart',
function(toState){
if(toState !== 'login')
UsersService.redirect();
})
我将其放在.run()中,每次路由更改时我都可以成功注销toState.但是,我似乎找不到具有我们将要到达的状态名称的属性.如果有人可以告诉我在哪里找到的,我认为我应该状况良好.
I put this in .run() and i can successfully log out toState every time route changes. However, i can't seem to find the property which has the name of the state that we are going to. If someone can tell me where to find that, i think i should be in good shape.
推荐答案
最终解决了这个问题,它满足了我的要求.
Ended up with this and it does what i want.
$rootScope.$on('$stateChangeStart',
function(event, toState, toParams, fromState, fromParams){
if(toState.name !== 'login' && !UsersService.getCurrentUser()) {
event.preventDefault();
$state.go('login');
}
});
这篇关于UI-路由器-每次路由更改时都运行功能-状态名称在何处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!