Search code examples
javascriptangularjsangular-http-interceptors

Shared AngularJS $http interceptors


I am wondering if Angular.js $http interceptors are shared thoughout the whole application.
Let's assume I have a myDependentApp module, shared between many apps. That module has some interceptor configured to control the $http requests/responses. I include that module by declaring it in application bootstrap:

angular.module('myApp', ['myDependentApp']);

And I have application template:

<html ng-app="myApp">

Are myDependentApp's interceptors going to be active in myApp?

Thanks for help.


Solution

  • The answer is yes, I tried it here:

    var dependentApp = angular.module('dependency',[]).config(['$httpProvider', function($httpProvider) {
        $httpProvider.interceptors.push(function ($q) {
            return {
                'request': function (config) {
                    console.log('request intercept');
                },
    
                    'response': function (response) {
                    console.log('response intercept');
                }
            };
        });
    }]);
    
    var app = angular.module('myapp', ['dependency']);
    app.controller('mycontroller', ['$scope', '$http', function ($scope, $http) {
        $http.get('http://www.google.com');
    }]);
    

    And I saw that the request was being intercepted. Here's the fiddle: http://jsfiddle.net/6dbgo6pt/1/