2016-07-26 87 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) 
{ 

}