2014-07-24 2 views
1

변화 상태 데이터가 서로 의존 UI 라우터 을 사용할 때 일부 데이터가 필요합니다, 그래서 난 체인 약속UI 라우터 해결 체인 약속

svc.getData1AndData2 = function(){ 
     var defer = $q.defer(); 
     $q.all([svc.getDatas1(),svc.getDatas2()]).then(function(values) { 
      $rootScope.datas2 = values[1]; 
      $rootScope.datas1 = values[0]; 
      defer.resolve(values); 
     }, function(error) { 
      console.log(error); 
      defer.reject(error); 
     }); 
    return defer.promise; 

svc.getData3Sources = function(){ 
     svc.getData1AndData2().then(function(value){ 
      return svc.getSources(24); 
     }) 
    }; 

svc.getSources=function(id){ 
     var defer = $q.defer(); 
     Services.query({Id:id}, function(data){ 
       defer.resolve(data); 
      }; 
    }; 

를 사용할 필요가 그리고 내 상태는

.state('secure.list', { 
     url: "/list", 
     templateUrl: "views/list.html", 
     controller: 'ListCtrl', 
     resolve: { 
       InitData: function (InitFactory) { 
       return InitFactory.getData3Sources(); 
       } 
     } 
}) 
입니다

반환 값은 정의되지 않습니다. 왜 그런지 누가 알 겠어?

답변

0

svc.getData3Sources는 일을하지 않습니다, 아니 ...

시도 ..

svc.getData1AndData2 = function(){ 
    // no need to use $defer here 

    var promise = $q.all([svc.getDatas1(),svc.getDatas2()]).then(function(values) { 
     $rootScope.datas2 = values[1]; 
     $rootScope.datas1 = values[0]; 

     return values; 
    }, function(error) { 
     console.log(error); 
     defer.reject(error); 
    }); 

    return promise; 
}; // this line was missing 

svc.getData3Sources = function(){ 
    var promise = svc.getData1AndData2().then(function(values){ 
     // changed value to values in the line above 
     // as the promise in getData1AndData2 resolves ... valueS 

     // why don't you use the values from getData1AndData2 ?? 
     return svc.getSources(24); 
    }); 

    return promise; 
}; 

svc.getSources=function(id){ 
    var defer = $q.defer(); 

    // it looks like you're having to use $q.defer 
    // as your Service.Query takes a callback rather than returning a promise 
    // if you control this then a promise would be much better 
    Services.query({Id:id}, function(data){ 
      defer.resolve(data); 
     }); // the bracket ')' here was missing 

    return defer.promise; // this line was missing 
}; 
+0

아무것도 반환하지 않습니다! 첫 번째 지연에서 반환하는 것은 무엇입니까? –

+1

이 작동한다는 것을 확인하면 getDatas1과 getDatas2가 약속을 반환합니까? .. 그렇다면 $ q.all은 단일 약속을 반환합니다 .. getData1AndData2에서 $ q.defer()를 사용할 필요가 없습니다 – stooboo

+0

코드는 얻은 값을 반환하지 않으므로 약간 이상합니다. getData3Sources는 getData1AndData2 'promise'등의 'values'를 사용하지 않습니다. – stooboo