AngularJS - wait for multiple resource queries to complete

前端 未结 3 1556
遥遥无期
遥遥无期 2020-11-29 17:57

I have a single factory defined with ngResource:

App.factory(\'Account\', function($resource) {
    return $resource(\'url\', {}, {
        query: { method:          


        
3条回答
  •  借酒劲吻你
    2020-11-29 18:25

    You'll want to use promises and $q.all().

    Basically, you can use it to wrap all of your $resource or $http calls because they return promises.

    function doQuery(type) {
       var d = $q.defer();
       var result = Account.query({ type: type }, function() {
            d.resolve(result);
       });
       return d.promise;
    }
    
    $q.all([
       doQuery('billing'),
       doQuery('shipping')
    ]).then(function(data) {
       var billingAccounts = data[0];
       var shippingAccounts = data[1];
    
       //TODO: something...
    });
    

提交回复
热议问题