我一直在寻找某种方法来知道两个或多个 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();你可以阅读它here .
这是一个片段:
all (promises)
Combines multiple promises into a single promise that is resolved when all of the input promises are resolved.
Parameters promises – {Array.} – An array of promises. Returns {Promise} – Returns a single promise that will be resolved with an array of values, each value corresponding to the promise at the same index in the promises array. If any of the promises is resolved with a rejection, this resulting promise will be resolved with the same rejection.
在版本 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);
});
关于javascript - 用于 AngularJS 的 jQuery "$.when().done()",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17414093/