javascript AngularJS + $q,在多个ajax调用完成后做一些事情
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25055691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AngularJS + $q, Do something after multiple ajax calls have finished
提问by Christos Baziotis
I need to load some data on page load and then do execute a task. in order to get the data i want i execute multiple different ajax calls. But in order to execute the task, i need all to make sure that all the ajax calls have finished. Here is what i have done so far:
我需要在页面加载时加载一些数据,然后执行任务。为了获得我想要的数据,我执行了多个不同的 ajax 调用。但是为了执行任务,我需要确保所有 ajax 调用都已完成。这是我到目前为止所做的:
$q.when(
$http.get('url1').success(function (data) {
$scope.data1 = data;
console.log("ajax1 finished");
}),
$http.get('url2').success(function (data) {
$scope.data2 = data;
console.log("ajax2 finished");
}),
$http.get('url3').success(function (data) {
$scope.data3 = data;
console.log("ajax3 finished");
}),
$http.get('url4').success(function (data) {
$scope.data4 = data;
console.log("ajax4 finished");
})
).then(
console.log("All ajax calls have finished!"),
executeTask()
);
My problem is that the code in block then(...);
is not executed after allthe ajax call have finished. I get something like this in my console:
我的问题是所有ajax调用完成后块中的代码then(...);
没有执行。我在控制台中得到这样的信息:
ajax2 finished
ajax1 finished
All ajax calls have finished!
ajax3 finished
ajax4 finished
I must be doing something wrong. How can i make it work the way i want?
我一定做错了什么。我怎样才能让它按照我想要的方式工作?
Edit: I tried the following like it was mentioned in the answers but i still face the same problem.
编辑:我尝试了以下内容,就像答案中提到的那样,但我仍然面临同样的问题。
$q.all([
$http.get('url1').then(function (data) {
$scope.data1 = data;
console.log("");
}),
$http.get('url2').success(function (data) {
$scope.data2 = then;
console.log("ajax2 finished");
}),
$http.get('url3').then(function (data) {
$scope.data3 = data;
console.log("ajax3 finished");
}),
$http.get('url4').then(function (data) {
$scope.data4 = data;
console.log("ajax4 finished");
})
]).then(
console.log(""),
executeTask()
);
回答by maurycy
I've made a working plunker for you with use of $q.all()
我已经为你制作了一个工作 plunker 使用 $q.all()
http://plnkr.co/edit/JHd3XPTKBxx4KRso5JRB?p=preview
http://plnkr.co/edit/JHd3XPTKBxx4KRso5JRB?p=preview
$q.all([
$http.jsonp('http://ip.jsontest.com/?callback=JSON_CALLBACK').then(function(response) {
$scope.ip.one = response.data
console.log('one')
}),
$http.jsonp('http://ip.jsontest.com/?callback=JSON_CALLBACK').then(function(response) {
$scope.ip.two = response.data
console.log('two')
}),
$http.jsonp('http://ip.jsontest.com/?callback=JSON_CALLBACK').then(function(response) {
$scope.ip.three = response.data
console.log('three')
}),
$http.jsonp('http://ip.jsontest.com/?callback=JSON_CALLBACK').then(function(response) {
$scope.ip.four = response.data
console.log('four')
}),
]).then(function() {
console.log('all')
})
回答by Cétia
You have two solutions :
您有两种解决方案:
You want to use
$q.all()
in order to wait for multiple promise to resolve. Example : http://jsfiddle.net/ThomasBurleson/QqKuk/Use the
resolve
mecanism of routeProvider in order to pre-load promise before rendering your screen : https://docs.angularjs.org/api/ngRoute/provider/$routeProvider
您要使用
$q.all()
以等待多个 promise 来解决。示例:http: //jsfiddle.net/ThomasBurleson/QqKuk/resolve
在渲染屏幕之前使用routeProvider 的机制来预加载承诺:https://docs.angularjs.org/api/ngRoute/provider/$routeProvider