2017-09-06 60 views
0

我已經有一個home頁面,現在我想要添加一個頁面,由localStorage變量alreadyShown控制。該設置是在homePlus首次顯示後,我們將alreadyShown設置爲true,因此homePlus的所有後期加載都將直接重定向到home頁面。我有以下代碼:根據一些條件直接重定向到一個頁面

.state('homePlus', { 
    url: '/homePlus', 
    templateUrl: '/htmls/homePlus.html', 
    controller: 'homePlusCtrl', 
    resolve: { 
     checkAlready: ['$window', function ($window) { 
      if ($window.localStorage['alreadyShown'] === "true") { 
       $window.location.href = "https://localhost:3000/home" 
      } 
     }] 
    } 
}) 

上面的代碼不檢查alreadyShown。但是,即使alreadyShowntrue,在重定向到home之前,它仍然顯示秒的homePlus頁。理想情況下,在這種情況下,我根本不想看到homePlus頁面。

有誰知道如何做到這一點?

回答

0

ui路由器事件$stateChangeStart應該工作。
angular.module.run

app.run(['$rootScope' function ($rootScope) {  
     $rootScope.$on('$stateChangeStart', function (event, to, toParams, from, fromParams) { 
      ...do something 
     }); 
    }]); 
+0

它的工作原理,也不要忘了加上'event.preventDefault()'... – SoftTimur