【发布时间】:2018-09-08 02:18:49
【问题描述】:
如果未经授权的用户尝试访问受限状态,则受限状态会在$state.go(fromState.name) 发回之前加载。似乎event.preventDefault(); 没有触发?
$rootScope.$on('$stateChangeStart',
function (event, toState, toParams, fromState, fromParams) {
if (toState.name == 'app.admin' || toState.name == 'app.bonus') {
AuthService.isAuthenticated().then(function (response) {
if (!response) {
event.preventDefault();
$rootScope.$broadcast(AUTH_EVENTS.notAuthenticated);
} else {
event.preventDefault();
if ('data' in toState && 'authorizedRoles' in toState.data) {
var authorizedRoles = toState.data.authorizedRoles;
if (!AuthService.isAuthorized(authorizedRoles)) {
$state.go(fromState.name, {}, {
reload: true
});
$rootScope.$broadcast(AUTH_EVENTS.notAuthorized);
}
}
}
}, function () {
event.preventDefault();
$rootScope.$broadcast(AUTH_EVENTS.notAuthenticated);
});
}
});
【问题讨论】:
标签: angularjs angular-ui-router