UI- 路由器 -- 在每次路由更改时运行函数 -- 状态名称在哪里? [英] UI- Router -- run function on every route change -- where does the state name live?
本文介绍了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- 路由器 -- 在每次路由更改时运行函数 -- 状态名称在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文