2016-09-07 33 views
0

我想檢查是否有用戶登錄,因爲它重定向到另一個頁面,如果沒有用戶,我想重定向到homePage。主頁有一個名爲storeCtrl的控制器。設置多個控制器在一個狀態,IONIC

我嘗試這樣做:

.state('homePage', 
    { 
     url: '/homePage', 
     templateUrl: 'templates/homePage/homePage.html', 
     controller:'storeCtrl', 
     function ($localStorage,$state) { 
     if ($localStorage.user) { 
      $state.go('tabs.profile'); 
      } else { 
      $state.go('homePage'); 
      } 
     } 

})

,但它不工作。我想這也:

.state('homePage', 
    { 
     url: '/homePage', 
     templateUrl: 'templates/homePage/homePage.html', 
     controller: 
     function ($localStorage,$state) { 
     if ($localStorage.user) { 
      $state.go('tabs.profile'); 
      } else { 
      $state.go('homePage'), 
      controller:storeCtrl 

      } 
     } 

})

但也沒有工作。

回答

0

而不是將其置於該狀態。嘗試添加這裏面app.run

app.run(function($ionicPlatform,$state,$rootScope,$localStorage) { 
    $ionicPlatform.ready(function() { 
    //your code 
    }) 
     $rootScope.$on('$stateChangeStart', function (event,next, nextParams, fromState,fromParams) { 
     var nextScreen=next.name; 
     if (nextScreen!="homePage" && $localStorage.user===undefined) { 
     event.preventDefault(); 
     $state.go('homePage'); 
     return; 
     } 
    }); 
}) 
+0

是引發RangeError:最大調用堆棧大小超過 錯誤 – noor

+0

最大調用堆棧大小纔會來,如果有一個循環..那就是如果在根上的每次改變它在if塊內部。嘗試做一些調試。此外,我添加了state.go –

+0

後返回我試過,它不會返回錯誤,但它不起作用。我登錄並保存了用戶,但是當我刷新頁面時,即使用戶仍然保存,它仍然重定向到主頁而不是簡介頁面 – noor

相關問題