有人知道如何在不更改url的情況下更改ui-router狀態?如下面的代碼所示;在某些情況下,用戶需要重定向到403或401狀態。我希望能夠做到這一點重定向,而無需更改網址。UI路由器更改狀態而不更改url
問候, klmdb
// make sure authGetCurrent has ran before routing starts
$rootScope.$on("$locationChangeSuccess", function(event, next) {
event.preventDefault();
AuthService.loadCurrentAuth().then(function(){
$urlRouter.sync();
}, function(){
console.log("BIG ERROR!!!");
});
});
// Configures $urlRouter's listener *after* your custom listener
$urlRouter.listen();
$rootScope.$on("$stateChangeStart", function(event, toState, toParams, fromState, fromParams) {
var requiredLogin = (toState && toState.data ? toState.data.requiredLogin : false),
requiredGroupRights = (toState && toState.data ? toState.data.requiredGroupRights : false); // require the user to have at least one of these rights in the current group
if (requiredLogin && !AuthService.isLoggedIn()) {
event.preventDefault();
$state.transitionTo('401');
return;
}
if(requiredGroupRights){
var i,
hasRight = false;
for(i=0;i<requiredGroupRights.length;i++){
if(GroupService.checkGroupRights(toParams.groupId, requiredGroupRights[i])){
hasRight = true;
break;
}
}
if(!hasRight){
event.preventDefault();
$state.transitionTo('403');
return;
}
}
});
謝謝,這可以幫助我很多! – klmdb 2014-12-08 11:34:33
這不適合我。 – 2016-08-26 13:43:09