对于AngularJS,jQuery是“$ .when()。done()”

我正在寻找一些方法来了解两个或多个ajax调用何时完成使用AngularJS,但我只能使用jQuery找到它:

$.when(promise3, promise5).done( function(threeSquare, fiveSquare) { console.info(threeSquare, fiveSquare); } ); 

和:

 var promises = [ $.getJSON('square/2'), $.getJSON('square/3'), $.getJSON('square/4'), $.getJSON('square/5') ]; $.when.apply($, promises).done(function() { console.info(arguments); }); 

有没有办法使用AngularJS做这样的事情? 感谢您的支持!

你会想看看$ q.all(); 你可以在这里阅读它。

这是一个片段:

所有(承诺)

将多个promise组合成单个promise,在解析所有输入promise时解析。

参数promises – {Array。} – 一系列承诺。 返回{Pr​​omise} – 返回将使用值数组解析的单个promise,每个值对应于promises数组中相同索引处的promise。 如果任何承诺通过拒绝得到解决,那么这个承诺将以相同的拒绝解决。

在1.1.x版中,您应该可以执行以下操作:

 $q.all([ $http.get('square/2'), $http.get('square/3'), $http.get('square/4'), $http.get('square/5') ]).then( function(arrayOfResponses) { $log.info('all set'); $log.debug(arrayOfResponses); }); 

更新:

从1.1.6版开始,您应该能够执行以下操作:

 var Square = $resource('square/:id', {id:1}); $q.all([ Square.get({id:2}).$promise, Square.get({id:3}).$promise, Square.get({id:4}).$promise, Square.get({id:5}).$promise ]).then(function(arrayOfResponses) { $log.info('all set'); $log.debug(arrayOfResponses); });