Reputation: 388436
I'm trying to convert a module written using jQuery to AngularJS.
I have an ajaxSuccess
and ajaxError
handler which does an ajax response processing at the global level. It responsible for showing success/failure messages across all ajax requests.
Is there a equivalent for this in AngularJS?
I've gone through the $http
service, but haven't found any solutions.
Upvotes: 4
Views: 403
Reputation: 60416
You can achieve this with interceptors. Example:
myapp.config(function($httpProvider) {
var myInterceptor = ['$rootScope','$q', function(scope, $q) {
function onSuccess(response) {
return response;
}
function onError(response) {
return $q.reject(response);
}
return function(promise) {
return promise.then(onSuccess, onError);
};
}];
$httpProvider.responseInterceptors.push(myInterceptor);
});
This will capture all your angular $http and $resource calls and call onError or onSuccess before continuing with your custom callbacks.
Upvotes: 3