2014-09-01 58 views
1

如果沒有該狀態的數據,阻止用戶進入狀態的最佳方式是什麼?如果在其他狀態之一中沒有數據,我需要將用戶重定向到/精選。如何在狀態下訪問我的控制器中的數據

我以爲我可以先檢查控制器的數據,然後使用$ state.go重定向,如果數據爲空,但我找不到如何訪問onEnter事件內控制器數據的示例

我認爲一個代碼例子應該概括:

fseControllers.config(function($stateProvider, $urlRouterProvider){ 

    $urlRouterProvider.otherwise("/featured"); 

    $stateProvider 
    .state('featured', { 
     url: "/featured", 
     templateUrl: "featured-template" 
    }) 

    .state('friends', { 
     url: "/friends", 
     templateUrl: "friends-template", 
     controller: 'FseController', 
     onEnter: function(){ 
      // Here I want to redirect to /featured if no data in controller 
     } 
    }) 

    .state('stories', { 
     url: "/stories", 
     templateUrl: "stories-template" 
    }) 

    .state('events', { 
     url: "/events", 
     templateUrl: "events-template" 
    }) 
}) 

回答

2

你可以重構你的控制器使用服務,可注入的OnEnter功能和控制器。 AngularJS Services

onEnter:function(FriendsService, $location){ 
    if(!!FriendsService.getFriends()){ 
    $location.path("/featured"); 
    } 
} 
相關問題