Json 在登录之前限制对视图的访问
我想知道在用户登录之前如何限制对任何和所有视图(登录/注册除外)的访问Json 在登录之前限制对视图的访问,json,angularjs,ionic-framework,Json,Angularjs,Ionic Framework,我想知道在用户登录之前如何限制对任何和所有视图(登录/注册除外)的访问 然后,我将使用$http.post将表单内容发布到我的后端,如果用户登录时没有问题,则重定向到仪表板视图。如果未登录,则可以解析为重定向 config(['$routeProvider', function ($routeProvider) { $routeProvider .when('/restricted',{ 'templateUr
然后,我将使用$http.post将表单内容发布到我的后端,如果用户登录时没有问题,则重定向到仪表板视图。如果未登录,则可以解析为重定向
config(['$routeProvider', function ($routeProvider) {
$routeProvider
.when('/restricted',{
'templateUrl': //some template url,
//Etc...
//then ...
'resolve': {
'onEnter': function (redirectService, authService) {
if (!!!authService.isLogged()) {
redirectService.redirectNotlogged();
}
}
}
});
}]);
我建议使用
$stateChangeStart
事件来侦听视图的更改,并检查用户是否登录。由于爱奥尼亚使用ui路由器
而不是Angular的默认$route
,因此您需要使用ui路由器
的API
$state
使用声明状态的解析功能需要向任何要保护的路由添加解析。使用事件侦听器可以让您在一个地方处理它。遵循此链接,我认为这将有助于您:
$rootScope.$on('$stateChangeStart',
function(event, toState, toParams, fromState, fromParams){
event.preventDefault();
// Check if user is logged in here, if not redirect state
if (!User.valid) {
$state.go('home.login');
}
});