AngularJS $resource interceptors AngularJS $resource interceptors angularjs angularjs

AngularJS $resource interceptors


To use an interceptor in a resource you should:

1 - Make an httpInterceptor with you request, response, responseError:

app.factory('myInterceptor', function () {    //Code    //return { request:...,});

2 - Config this Interceptor in your app:

app.config(['$httpProvider', function ($httpProvider) {    $httpProvider.interceptors.push('myInterceptor');}]);

Right now as you have config your httpProvider to has an interceptor wherever you inject $http you will use this provider so... you will excute your request, response and responseError funciton.

3 - Using it in a resource.As $resource use $http and you have config a httpProvider globaly you will call your interceptors' func when you use your resource.

Second question:You can not set an interceptor to a concrete $http object, they (interceptors) are set globally.

(Even if you set the interceptor before your module definition and then you remove it, you can not know the execution order)

What you can do if you do not want to override the interceptor property in each $resource action (as you write in your question) you can improve your interceptor.

app.factory('userLoadingInterceptor', function () {    //Code    return {        request: function(){            //Check if your are working with a url related with users            // and if so do things...        }});


From the docs:

The interceptor object has two optional methods - response and responseError

I don't know what you want to achieve but generic HTTP interceptors might be an alternative.


A generic HTTP Interceptor should do what you want. You can find a sample here: Handle HTTP 302 response from proxy in angularjs.