AngularJS + $ q, zrób coś po zakończeniu wielu wywołań ajax
Muszę załadować dane podczas ładowania strony, a następnie wykonać zadanie. aby uzyskać potrzebne dane, wykonuję kilka różnych wywołań Ajax. Ale aby ukończyć zadanie, muszę się upewnić, że wszystkie wywołania ajax są zakończone. Oto, co zrobiłem do tej pory:
$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()
);
Mój problem polega na tym, że kod w bloku
then (...);nie jest wykonywany
w sumie
Ajax. W mojej konsoli pojawia się coś takiego:
ajax2 finished
ajax1 finished
All ajax calls have finished!
ajax3 finished
ajax4 finished
Muszę robić coś złego. Jak mogę to zrobić tak, jak chcę?
Edit
: Próbowałem wykonać następujące czynności, jak wspomniano w odpowiedziach, ale nadal mam ten sam 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());
Nie znaleziono powiązanych wyników
Zaproszony:
Aby odpowiedzieć na pytania, Zaloguj się lub Zarejestruj się
2 odpowiedzi
Anonimowy użytkownik
Potwierdzenie od:
Anonimowy użytkownik
Potwierdzenie od:
http://plnkr.co/edit/JHd3XPTKB ... eview