使用AngularJs 1.6.5我使用for循环进行多个$ http GET调用.这是代码: -
for(var i=0; i < 11; i++){
var url = "/api/learner_qa/" + i;
$http.get(url).then(successCallback, errorCallback);
function successCallback(response){
console.log(response);
};
function errorCallback(error){
console.log(error);
};
};
Run Code Online (Sandbox Code Playgroud)
我想要做的是在完成所有GET请求时触发一个函数.
你需要一次解决多个promises的时候可能会出现这种情况,这很容易通过
$q.all()传入一个数组或一个promises对象来实现,这.then()两个promises会在解析后调用:
你可以把一个array和push你http通话到它
var array = [];
for(var i=0; i < 11; i++){
var url = "/api/learner_qa/" + i;
array.push($http.get(url))
};
$q.all(array).then(function(response) {
console.log(response)
}).catch(function(error){
console.log(error)
});
Run Code Online (Sandbox Code Playgroud)
使用它code,response一旦你requests成功,就会有了.
| 归档时间: |
|
| 查看次数: |
51 次 |
| 最近记录: |