在角度和ui路由器上加速
如果不满足前提条件,就要努力重定向到不同的状态:
我尝试使用拦截器:( 如何在角度拦截器中预先形成重定向).
但有人提到处理$ stateChangeState会更合适.但我仍然遇到无限循环:
/**
* Check here for preconditions of state transitions
*/
$rootScope.$on('$stateChangeStart', function(event, toState) {
// which states in accounts to be selected
var accountRequiredStates = ['user.list', 'user.new'];
if(_.contains(accountRequiredStates, toState.name)){
event.preventDefault();
ApiAccount.customGET('get_current').then(function(resp){
// if I have a selected account, go about your business
if(resp.hasOwnProperty('id')){
$state.go(toState.name);
} else { // prompt user to select account
$state.go('user.select_account');
}
})
}
});
Run Code Online (Sandbox Code Playgroud)
任何人都可以建议一个更好的模式(一个有效)
谢谢!
注意:类似的问题不同的方法:如何在角度拦截器中预先形成重定向