Is there a second success in angular $ q.all () as in jQuery $ .get ()
Looking at the jQuery documentation I found the following:
$.get( "example.php", function() {
alert( "success" );
})
.done(function() {
alert( "second success" ); <---
})
.fail(function() {
alert( "error" );
})
.always(function() {
alert( "finished" );
});
I need to execute a method in an Angular controller after several ajax calls like:
$q.all([
$http.get(ROOT + "Lookup/GetStates"),
$http.get(ROOT + "Lookup/GetCountries"),
$http.get(ROOT + "Address/GetAddresses"),
]).then(function (results) {
$scope.states = jQuery.parseJSON(results[0].data.data);
$scope.country = jQuery.parseJSON(results[1].data.data);
$scope.addresses = jQuery.parseJSON(results[3].data);
});
after execution then
is executed (only after that) I need to call the method$scope.setupControls()
Is there an opportunity for this?
source to share
Look at this:
.finally(function() {
// do this on both error and success
});
As the saying goes, executed after success and error
So the complete code is:
$q.all([
$http.get(ROOT + "Lookup/GetStates"),
$http.get(ROOT + "Lookup/GetCountries"),
$http.get(ROOT + "Address/GetAddresses"),
]).then(function (results) {
$scope.states = jQuery.parseJSON(results[0].data.data);
$scope.country = jQuery.parseJSON(results[1].data.data);
$scope.addresses = jQuery.parseJSON(results[3].data);
}).finally(function() { /* <-- here */
// do this on both error and success
});
source to share
Why don't you do this:
$q.all([
$http.get(ROOT + "Lookup/GetStates"),
$http.get(ROOT + "Lookup/GetCountries"),
$http.get(ROOT + "Address/GetAddresses"),
]).then(function (results) {
$scope.states = jQuery.parseJSON(results[0].data.data);
$scope.country = jQuery.parseJSON(results[1].data.data);
$scope.addresses = jQuery.parseJSON(results[2].data);
$scope.setupControls();
});
I think there is no need for a second success. Keep it simple (and sexy?)
source to share
You should be able to call multiple times.
https://docs.angularjs.org/api/ng/service/$q
Chaining promises Because calling the then-then method returns a new derived promise, it is easy to chain promises:
promiseB = promiseA.then(function(result) {
return result + 1;
});
// promiseB will be resolved immediately after promiseA is resolved and its value
// will be the result of promiseA incremented by 1
$q.all([
$http.get(ROOT + "Lookup/GetStates"),
$http.get(ROOT + "Lookup/GetCountries"),
$http.get(ROOT + "Address/GetAddresses"),
]).then(function (results) {
$scope.states = jQuery.parseJSON(results[0].data.data);
$scope.country = jQuery.parseJSON(results[1].data.data);
$scope.addresses = jQuery.parseJSON(results[3].data);
}).then(function (result) {
//do something here
}) ;
source to share
Angular $ q provides the same methods as jQuery. Details here .
One approach I could think of is this:
$http.get('parent request')
.then(function(win){
// do something when everything is resolved
},
function(err){
//handle error
},
function(progress){
//do something when a child request is resolved
}
);
... where the "parent request" does some child requests and for each allowed child request you can use the method .notfiy()
to update the parent request and also you have to keep track of the entire status of your child request and when everything is allowed you can call .resolve()
and do whatever you want ( $scope.setupControls()
in your case).
source to share