0
在我的应用程序中,我需要检查用户的详细信息,如果用户没有登录,那么我需要重定向回/login
页面。但我的尝试不工作,我应该在哪里做这个配置?
我尝试:
(function(){
"use strict";
angular.module('meanOffice')
.config(routeConfig)
.run(stateController);
function routeConfig($locationProvider, $stateProvider, $urlRouterProvider){
$stateProvider
.state('login', {
url:'/',
templateUrl : 'app/views/pages/login/login.html',
controller : 'mainController as main'
})
.state('users', {
url:'/users',
templateUrl : 'app/views/pages/users/users.html',
controller : 'usersController as users',
required : true,
resolve : {
usersData : function(){
return "Arif";
}
}
})
$urlRouterProvider.otherwise('/');
}
function stateController($rootScope, auth, authToken){
$rootScope.$on('$routeChangeStart', function(){
var isLoggedIn = auth.isLoggedIn();
//but each time the route change, this is not called..!?
if(!isLoggedIn)
$location.path('/');
});
}
})();
得到任何错误?又该auth.isLoggedIn()返回? –
它根据值返回true或false。但即使我没有触发'routeChageStart'在所有,每次 – user2024080
好吗你确定状态控制器函数没有调用?如果你确实尝试使这个运行方法的方法在接受的答案中提到http:///stackoverflow.com/questions/20969835/angularjs-login-and-authentication-in-each-route-and-controller –