当应用程序最初加载时,我在$ stateChangeStart处理程序中获得了一个无限的摘要循环。
我已经查看了当前的帖子。虽然我的问题似乎正如这些帖子所述,但提出的解决方案和答案都没有解决我的问题。我不是在尝试解决授权主题,我正在尝试根据用户的角色解析去路由的权限。
在定义路径时,我将以下对象添加到$ state对象
$stateProvider.state('routeName', {
// ...
authorization: {
allowAll: false,
allowRoles: [
'admin',
'power',
'user',
'guest'
],
denyAll: false,
denyRoles: [{
role: 'user',
route: 'dashboard',
data: {
//...
}
},{
role: 'power',
route: 'dashboard',
data: {
//...
}
}],
failover: "home"
}
});
我将此作为$ stateChangeStart的处理程序
$rootScope.$on('$stateChangeStart', function (e, toState, toParams, fromState, fromParams) {
var userRole = UserFactory.role;
// default to app route if route 'failover' not defined
var failOver = toState.authorization.failover || 'home';
// User is allowed to continue through to the state.
if (toState.authorization.allowAll ||
_.contains(toState.authorization.allowRoles, userRole)) {
// Do nothing user is allowed this route
return;
} else if (!toState.authorization.denyAll &&
toState.authorization.denyRoles.length > 0) {
// User is denied this route find the route to redirect to
var entry = _.find(toState.authorization.denyRoles, function (item) {
return item.role == userRole;
});
if (entry) {
e.preventDefault();
$state.go(entry.route, entry.data || { });
return;
}
}
// All deny conditions not specifically targeted
e.preventDefault();
$state.go(failOver);
});
以下是完整的plunker。