且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

等待所有$ http请求的角JS完成

更新时间:2021-08-08 05:02:15

$ HTTP 电话始终返回可与 $ Q使用的承诺。所有功能。

$http call always returns a promise which can be used with $q.all function.

var one = $http.get(...);
var two = $http.get(...);

$q.all([one, two]).then(...);

借助文档是pretty明确一下:

The documentation is pretty clear about it:

所有的(承诺)

承诺 - 承诺的数组或哈希

promises - An array or hash of promises.

在你的情况,你需要创建一个数组和所有的通话推入它的循环:

In your case you need to create an array and push all the calls into it in the loop:

var arr = [];

for (var a = 0; a < subs.length; ++a) {
    arr.push($http.get(url));
}

$q.all(arr).then(function (ret) {
    // ret[0] contains the response of the first call
    // ret[1] contains the second response
    // etc.
});