![](/img/trans.png)
[英]Does jquery $.when().done() wait for all $.ajax().done()'s to complete?
[英]jQuery's “$.when().done()” for AngularJS
我正在尋找一些方法來了解兩個或多個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。} - 一系列承諾。 返回{Promise} - 返回將使用值數組解析的單個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);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.