Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.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
Angularjs 正在等待$http结束以执行$q.all_Angularjs - Fatal编程技术网

Angularjs 正在等待$http结束以执行$q.all

Angularjs 正在等待$http结束以执行$q.all,angularjs,Angularjs,在下面的代码中,我根据变量x向承诺数组添加函数。在最后一个条件中,我只需要在$http响应之后添加函数。这个代码正确吗?$q.all中所有承诺的执行是否会等待$http函数返回响应 var promises = []; array.forEach(function(x){ if (x==1) promises.push(function1('aaa')); else if (x==2) promises.push(function2('bbb'

在下面的代码中,我根据变量
x
向承诺数组添加函数。在最后一个条件中,我只需要在$http响应之后添加函数。这个代码正确吗?
$q.all
中所有承诺的执行是否会等待
$http
函数返回响应

var promises = [];

array.forEach(function(x){

    if (x==1)
        promises.push(function1('aaa'));
    else if (x==2)
        promises.push(function2('bbb'));
    else {
          $http.get("url.htm").then(function(response) {
              promises.push(function3(response));
          });
    }
});

$q.all(promises).then(function(resultArray) { 
   // .....
}

它不会等待AJAX响应,因为它是异步的

还不清楚Function1和function2返回什么(承诺?),但它应该更像

var promises = [];

array.forEach(function(x){

    if (x===1)
        promises.push(function1('aaa'));
    else if (x===2)
        promises.push(function2('bbb'));
    else {
          promises.push($http.get("url.htm"))
    }
});

$q.all(promises).then(function(resultArray) { 
   // .....
}

是的,函数1、2和3返回承诺,但是在您的解决方案中,您没有调用
function3
,您应该在$q.all回调中调用它