Is there a jQuery.ajaxSuccess equivalent for AngularJS

I am trying to convert a module written using jQuery to AngularJS.

I have an ajaxSuccess and ajaxError handler that processes the ajax response globally. It is responsible for displaying success / failure messages in all ajax requests.

Is there an equivalent for this in AngularJS?

I went through the $http service but did not find any solutions.

+4
source share
1 answer

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 of your angular $ http and $ resource calls and raise onError or onSuccess before continuing with your custom callbacks.

+3
source

All Articles