2016-07-26 29 views
1

在我的角度应用程序中,我使用$ routeProvide.otherwise函数将用户重定向到'/ home'路线。但是,如果用户没有登录,那么我重定向使用$ routeChangeStart到登录页面 -

$rootScope.$on('$routeChangeStart', function(event, next, current) { 
    console.log('$routeChangeStart'); 
    $rootScope.layout.loading = true; 

    if ($rootScope.loggedIn === false) { 

    if (next.templateUrl === "login/login.html") { 
     // in order to skip this behavior when already navigating to the login page, 
     // we have to explicitly check the next templateUrl 
    } else { 
     console.log('DENY: redirecting to login'); 
     //event.preventDefault(); 
     $location.path('/login'); 
    } 
    } else { 
    console.log('ALLOW'); 
    } 

}); 

我在$ routeChangeStart被称为两次控制台注意。无法弄清楚为什么!

Here is the working Plunker

+0

第一时间被称为当你打开的网页,并试图去“家”。第二次是当你重定向到登录页面。 – DevTrong

+0

防止双重电话的任何解决方案?在我的实际应用程序中,重定向登录页面实际上正在发生两次,由于这一点。 – Rishabh

+0

在您的家庭控制器中添加$ rootScope。$ on事件(而不是在.run()中),并在重定向到登录页面时取消注册事件。 – DevTrong

回答

-1

尝试

$scope.$on('$locationChangeStart', function(event, next, current) 
{ 

}