Alex Rivera | Logout

Implementing loading spinner using httpInterceptor and AngularJS 1.1.5

Asked 2013-07-24T15:38:28.310
40

I've found an example of a loading spinner for http/resource calls here on SO:

As you can see the implementation works (using AngularJS 1.0.5). However if you change the sources to AngularJS 1.1.5. The example does not work anymore.

I learned that $httpProvider.responseInterceptors is deprecated in 1.1.5. Instead one should use $httpProvider.interceptors

Unfortunately just replacing the above string in the Plunker did not solve the problem. Has anyone ever done such a loading spinner using HttpInterceptor in AngularJS 1.1.5?

Thanks for your help!

Michael

Edit
Report

1 Answer

18

"responseInterceptors" was deprecated. "interceptors" replaced it with many enhancements in a preview version. Off the top of my head I don't remember which version. Documentation on this is sparse, so you're probably best off examining the source code.

The gist of the change looks like this:

$httpProvider.interceptors.push(function($q, $rootScope) {
  return {
     'request': function(config) {
        // intercepts the request
     },
     'response': function(response) {
       // intercepts the response. you can examine things like status codes
     },
     'responseError': function(response) {
       // intercepts the response when the response was an error
     }
  }
});

In the angular source you will find documentation under "* # Interceptors" in the $HttpProvider function. There is an example usage very similar to what I posted above.

answered 2013-07-24T17:39:22.893

Your Answer