2015-06-30 85 views
0

我的問題是相似的渲染頁面,讓這一個:stop angular-ui-router navigation until promise is resolved只有當異步方法完成

儘管如此,沒有一個答案似乎來解決這個問題。

我的應用程序存儲一個帶有令牌的cookie。當頁面被加載時(例如在F5之後),應用程序檢查cookie,如果找到,它使用令牌加載從Web服務加載的用戶設置。需要這些設置才能從我的控制器將調用的其他Web服務獲取所有其他信息,因此必須在頁面呈現之前加載設置。

這是我當前的代碼:

$rootScope.$on('$stateChangeStart', function(e, toState, toParams, fromState, fromParams) { 
     e.preventDefault(); 

     authentication.checkCookie(function() { 
      if (toState.name == 'login'){ 
       if(authentication.getLoginData()) { 
        //e.preventDefault(); 
        $state.go('pricelist'); 
       } 
      } else { 
       if(!authentication.getLoginData()) { 
        //e.preventDefault(); 
        $state.go('login'); 
       } 
      } 

      // No redirect is needed - now render the page requested 
      $urlRouter.sync(); 
     }); 

    }); 

不幸的是,這個代碼不工作:它只是導致無限循環。

我怎樣才能確保只要檢查cookie並加載用戶設置,渲染就會繼續(這也可以在checkCookie中完成)?

+0

檢查[ui-router resolve](https://github.com/angular-ui/ui-router/wiki#resolve) – Fissio

回答

0

我設法通過在提到鏈接使用另一種答案來解決這個問題:

$rootScope.$on('$stateChangeStart', function(e, toState, toParams, fromState, fromParams) { 
     if ($rootScope.stateChangeBypass) { 
      $rootScope.stateChangeBypass = false; 
      return; 
     } 

     e.preventDefault(); 

     authentication.checkCookie(function() { 
      $rootScope.stateChangeBypass = true; 
      if (toState.name == 'login'){ 
       if(authentication.getLoginData()) { 
        $state.go('pricelist'); 
       } else { 
        $state.go(toState, toParams); 
       } 
      } else { 
       if(!authentication.getLoginData()) { 
        $state.go('login'); 
       } else { 
        $state.go(toState, toParams); 
       } 
      } 
     }); 

    }); 

這不是一個真正的乾淨的解決方案,但它的工作原理。