Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/411.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 等待所有$http请求在Angular JS中完成_Javascript_Angularjs - Fatal编程技术网

Javascript 等待所有$http请求在Angular JS中完成

Javascript 等待所有$http请求在Angular JS中完成,javascript,angularjs,Javascript,Angularjs,我有一个页面,可以根据变量的长度发出不同数量的$http请求,然后我只想在所有请求完成后将数据发送到作用域。对于这个项目,我不想使用jQuery,所以请不要在回答中包含jQuery。目前,当每个请求完成时,数据被发送到作用域,这不是我想要的 这是我到目前为止的部分代码 for (var a = 0; a < subs.length; a++) { $http.get(url).success(function (data) { for (var i = 0; i < da

我有一个页面,可以根据变量的长度发出不同数量的
$http
请求,然后我只想在所有请求完成后将数据发送到作用域。对于这个项目,我不想使用jQuery,所以请不要在回答中包含jQuery。目前,当每个请求完成时,数据被发送到作用域,这不是我想要的

这是我到目前为止的部分代码

for (var a = 0; a < subs.length; a++) {
  $http.get(url).success(function (data) {
    for (var i = 0; i < data.children.length; i++) {
      rData[data.children.name] = data.children.age;
    }
  });
}

感谢您的帮助。

$http
调用始终返回一个承诺,该承诺可与
$q.all
函数一起使用

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

$q.all([one, two]).then(...);
有关此行为的更多详细信息,请访问:

所有(承诺)

承诺-承诺的数组或散列

在您的情况下,您需要创建一个数组,并将所有调用推送到循环中。通过这种方式,您可以在阵列上使用
$q.all(…)
,方法与上述示例相同:

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.
});
var arr=[];
对于(变量a=0;a
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.
});