Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the equivalent of jQuery.when() in angular

In jQuery we can do $.when( $.ajax( "/page1.php" ), $.ajax( "/page2.php" ) ).done(function( a1, a2 ) { ... }); What's the equivalent in angular? I really need to wait for all ajax calls finish then do stuff. Thanks.

like image 537
stevemao Avatar asked Aug 16 '14 00:08

stevemao


2 Answers

You can use $q.all to handle multiple promises. Also, use $http to make the calls, that's more angular.

Here is a nice tutorial:

https://egghead.io/lessons/angularjs-q-all

Hope that helps.

like image 94
Fedaykin Avatar answered Sep 27 '22 23:09

Fedaykin


The equivalent would be:

$q.all([$http.get('/page1.php'),$http.get('/page2.php')]).then(function(values){
   var a1 = values[0];
   var a2 = values[1];
   ... 
});

AngularJS Documentation for $q

like image 29
pixelbits Avatar answered Sep 27 '22 23:09

pixelbits