所以我有这个非常奇怪的错误:我想检查用户是否在进入状态时登录并将其重定向回SignIn页面(如果不是)。所以在我的配置中我有:
.state('home', {
cache: false,
abstract: true,
url: "/home",
templateUrl: "app/home/home.html",
onEnter: function($state, MyFirebaseService) {
// check session
var userId = MyFirebaseService.LoginUserId();
if (!userId) {
$state.go('auth.signin')
};
}
})
所以我输入http://localhost:8100/#/home/courses进入课程页面而不登录,一切都很完美。用户重定向回auth.signin视图。但是当我再次输入地址栏http://localhost:8100/#/home/courses时,它会抛出4个错误:
TypeError: Cannot read property '@' of null
TypeError: Cannot read property 'auth-signin@auth' of null
TypeError: Cannot read property 'auth-signup@auth' of null
我的登录和注册是在抽象视图中调用auth。为什么这样以及如何解决?
答案 0 :(得分:0)
我个人在.run上执行认证控制的行为(与事件捕手:
pthread_mutexattr_setrobust
如果您在ui-router上添加一些授权角色控件,例如:
.run([
'$rootScope','authService','$q',
function($rootScope, authService,q) {
$rootScope.$on('$stateChangeStart', function(event, next, current) {
// YOUR CONTROL & REDIRECT
});
}]);
您可以使用var .state('home', {
url: "/home",
templateUrl: "includes/pages/homePage.html",
resolve : {
authorizedRoles : function(){ return [ USER_ROLES['su'],
USER_ROLES['user'],
USER_ROLES['admin'],
USER_ROLES['skyadmin'],
USER_ROLES['skyuser'],
USER_ROLES['skysu']
] }
}
})
轻松检入$stateChangeStart
并将其与您的用户角色进行比较