2016-06-16 126 views
2

我想把一些数据放在异步调用返回的作用域上。
我在工厂里有一个名为公司的数组。AngularJS等待单个异步调用

factory.getByCategoryId = function (id) { 
    $http.get('http://localhost/campaign?access-token=12345&id=2').then(
    function (result) { 
     factory.companies = []; 
     if (result.data.length !== 0) { 
     for (var index = 0; index < result.data.length; index++) { 
      factory.companies.push(result.data[index]); 
     } 
     } else { 
     console.log('Result is not set.', result); 
     } 
    }, 
    function (result) { 
     console.log('Failed', result); 
    } 
); 
} 

调用函数:

$scope.closeModal = function (index) { 
    if (index) { 
    var promises = [] 

    promises.push(Service.getByCategoryId($scope.categories[index].id)); 

    $q.all(promises).then(function() { 
     $scope.myItems = Service.all(); //return all from factory.companies 

     $scope.refreshItems(); //this function refreshes the DOM using myItems 
    }); 
    } 
} 

调用住就是同这个功能首次factory.companies但之后的第二调用它的更新。我敢肯定,应用程序应该等待更多,但不知道如何。

回答

1

我只会从你的服务

返回$http在你的工厂:

factory.getByCategoryId = function (id) { 
    return $http.get('http://localhost/campaign?access-token=12345&id=2'); 
} 

而且你的函数

$scope.closeModal = function (index) { 
    Service.getByCategoryId($scope.categories[index].id).then(
    function (result) { 
     if (result.data.length) { 
      angular.forEach(result.data, function(value) { 
       $scope.myItems.push(value); 
      }); 
      $scope.refreshItems(); 
     } else { 
      console.log('Result is not set.', result); 
     } 
    }, 
    function (result) { 
     console.log('Failed', result); 
    } 
}; 

或者你可以重写你的服务功能,以解决一个承诺:

factory.getByCategoryId = function (id) { 
    return $q(function (resolve, reject) { 
     $http.get('http://localhost/campaign?access-token=12345&id=2').then(
      function (result) { 
       factory.companies = []; 
       if (result.data.length !== 0) { 
        for (var index = 0; index < result.data.length; index++) { 
         factory.companies.push(result.data[index]); 
        } 
        resolve(factory.companies); 
       } else { 
        reject('Result is not set.', result); 
       } 
      }, 
      function (result) { 
       console.log('Failed', result); 
      } 
     ); 
    }); 
} 

并以此

$scope.closeModal = function (index) { 
    Service.getByCategoryId($scope.categories[index].id).then(function(result) { 
    $scope.myItems = result; 
    $scope.refreshItems(); 
}, function(result) { 
    console.log(result); 
}); 
+1

KöszönömAlex,sokatsegítettél! (: – MagicDragon

0
promises.push(Service.getByCategoryId($scope.categories[index].id)); 

所以你拿的Service.getByCategoryId返回值和pushpromises

Service.getByCategoryId的返回值是多少?

它没有return声明,所以它是undefined

您需要修改Service.getByCategoryId,以便它返回一个承诺(推测返回值为$http.get)。

+0

难道不就是时候了吗?我给了我的服务函数返回值,但我不需要它。我只需要工厂的补充阵列。我有同样的问题,它只适用于第二个电话。 – MagicDragon

1

如果

Service.getByCategoryId($ scope.categories [指数] .ID)

返回一个承诺,那么

$scope.closeModal = function (index) { 
if (index) { 
    Service.getByCategoryId($scope.categories[index].id).then(function (res) { 
     $scope.myItems = Service.all(); //return all from factory.companies 
     $scope.refreshItems(); //this function refreshes the DOM using myItems 
    }, function (res) { 
     // Error Handling 
    }) 
} 
} 

仅供参考::

app.factory('Service', function ($http) { 
return { 
    // Below function retuns a promsie 
    getByCategoryId_promise: function() { 
     // This simply sends the promise but you need to make it execute using $q 
     //or by entending like the closeModal method above 
     return $http.get("URL GOES HERE"); 
    }, 
    // Below function retuns results from a promise 
    getByCategoryId: function() { 
     // This Function waits till the HTTP call is resolved and then sends the result 
     return $http.get("URL GOES HERE").then(function (res) { }, function (res) { }) 
    }, 
} 
}) 
+0

感谢您的回答,我给了你一个UP,但由于我的低声誉,它还没有显示。 – MagicDragon