2013-12-20 2 views
1

다른 해결 매개 변수에 해결 매개 변수를 삽입 할 수 있는지 궁금합니다. 코드는 더 말할 것이다 : 나는 라우팅을 위해 각 UI 라우터를 사용하고
해결 방법으로 해결하기

.state('videos.videosLection', { 
     url : '/:lection', 
     templateUrl : '/partials/videosLection.html', 
     controller : 'VideosLectionCtrl', 
     menuItem : 'Videos', 
     resolve : { 
       selectedLection : function ($stateParams, lections) { 
         ... 
       }, 
       sections : function(Section, selectedLection) { 
         ... 
       }     
     } 
}) 

. 섹션 확인이 시작되기 전에 selectedLection을 가져야합니다.

아이디어가 있으십니까? 고마워요

+0

아니 도움이 희망 :-) 알려주세요. 상속을 해결할 때는 부모 상태에서 자식 상태로만 작동합니다. –

답변

0

selectedLection 조각을 서비스로 전환 할 수 있습니까? 그리고 나서 다음과 같이하십시오 :

resolve: { 
    sections: function(Section, selectedLectionService, $stateParams, lections){ 
     var result = selectedLectionService($stateParams, lections); 
     // do something with result here... 
    } 
} 

내가 필요한 모든 것을 수행 할 수 있는지는 잘 모르겠지만 올바른 방향으로 향하게 할 수는 있습니다. 그 트릭을하지 않으면 알려줘.

+0

문제는 어떤 이유로 서비스에서 $ stateParams에 액세스 할 수 없다는 것입니다. 따라서이 솔루션은 나에게 도움이되지 못했습니다. 아니면 뭔가 잘못되었습니다 :-) –

0


나는 마침내 그것을 알아 냈습니다. 여기에가는 길은 하나의 객체만을 해결하는 것입니다. 두 객체는 ​​그 자체로 약속입니다. 코드를 체크 아웃 :

resolve : { 
    data : function($stateParams, lections, Video, Section, $q) { 
     // defer for the data object 
     var defer = $q.defer(); 

     // selectedLection 
     var selectedLection = null; 

     // if there is lection selected 
     if($stateParams.lection) 
     { 
      // lection is selected 
      lections.forEach(function(lection) 
      { 
       // find the selected lection 
       if(lection.addressUrl == $stateParams.lection) 
       { 
        // save it 
        selectedLection = lection; 
       } 
      }); 
     } 
     else 
     { 
      // resolve it with everything empty 
      defer.resolve({ 
       selectedLection : null, 
       lectionVideos : [], 
       sections: [] 
      }); 
      return defer.promise; 
     } 

     // promise to the lection videos 
     var deflectionvideos = $q.defer(); 
     // find all the videos by the lection id 
     Video.GetAllByLection(selectedLection.id, null, function(data) { 
      // resolve it with the data 
      deflectionvideos.resolve(data); 
     }, function(error) { 
      // resolve it with empty array 
      deflectionvideos.resolve([]); 
     }); 

     // promise for the sections 
     var defsections = $q.defer(); 
     // find all the sections of selected lectino 
     Section.GetAllByLection(selectedLection.id, function(data) { 
      // resolve it with the data 
      defsections.resolve(data); 
     }, function(error) { 
      // resolve it with empty array 
      defsections.resolve([]); 
     }); 

     // wait, untill all the promises for sections and lectionsvideos are resolved 
     $q.all([ 
       deflectionvideos.promise, 
       defsections.promise 
      ]).then(function(data) { 
       // resolve it with actual data 
       defer.resolve({ 
        selectedLection : selectedLection, 
        lectionVideos : data[0], 
        sections: data[1] 
       }); 
      }); 

     // return promise for the data object 
     return defer.promise; 
    } 
} 

너희들이 다른 방법 또는 더 나은 솔루션이있는 경우

이 누군가

관련 문제