2014-09-01 3 views
1

해당 상태에 대한 데이터가없는 경우 사용자가 상태로 가지 않게하는 가장 좋은 방법은 무엇입니까? 다른 상태 중 하나에 데이터가 없으면 사용자를/featured로 리디렉션해야합니다.상태에서 내 컨트롤러의 데이터에 액세스하는 방법

데이터를 먼저 컨트롤러에서 검사하여 데이터가 null 인 경우 $ 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"); 
    } 
} 
관련 문제