Angular Resource calls and $q

前端 未结 4 795
失恋的感觉
失恋的感觉 2020-12-24 02:37

Folks,

I have my code setup somewhat as below:

$scope.init = function(){
  return $q.all([resource1.query(),resource2.query(),resource3.query()])
            


        
4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-24 03:03

    I ran into this problem, and it was quite confusing. The problem appears to be that calling a resource action doesn't actually return an http promise, but an empty reference (that is populated when the data returns from the server -see the return value section of the $resource docs).

    I'm not sure why this results in .then(result) returning an array of unresolved promises, but to get each resource's promise, you need to use resource1.query().$promise. To re-write your example:

    $scope.init = function() {
      return $q.all([resource1.query().$promise, resource2.query().$promise, resource3.query().$promise])
               .then( function(result) {
                 $scope.data1 = result[0];
                 $scope.data2 = result[1];
                 $scope.data3 = result[2];
    
                 console.log($scope.data1);
    
                 doSomething($scope.data1,$scope.data2); 
               })
    }
    

    I hope that saves someone else some time.

提交回复
热议问题