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());

Zaproszony:
Anonimowy użytkownik

Anonimowy użytkownik

Potwierdzenie od:

Masz dwa rozwiązania:
Anonimowy użytkownik

Anonimowy użytkownik

Potwierdzenie od:

Zrobiłem dla Ciebie działający planer za pomocą
$ q.all ()
http://plnkr.co/edit/JHd3XPTKB ... eview
http://plnkr.co/edit/JHd3XPTKB ... eview
$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')
})

Aby odpowiedzieć na pytania, Zaloguj się lub Zarejestruj się