2016-11-17 78 views
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('/'); 

     }); 


    } 

})(); 
+0

得到任何错误?又该auth.isLoggedIn()返回? –

+0

它根据值返回true或false。但即使我没有触发'routeChageStart'在所有,每次 – user2024080

+0

好吗你确定状态控制器函数没有调用?如果你确实尝试使这个运行方法的方法在接受的答案中提到http:///stackoverflow.com/questions/20969835/angularjs-login-and-authentication-in-each-route-and-controller –

回答

0

这是我的错,如果我使用$stateChageStart - 它工作正常。

这里是我的更新

$rootScope.$on("$stateChangeStart", function(){ 

    console.log('each time starts') 

})