Search code examples
javascriptangularjsangularjs-controllerangularjs-http

How to get different controllers to use different configurations of $httpProvider in AngularJS?


I have a AngularJS project using the directory layout from angular-seed. https://github.com/angular/angular-seed

app.js contains the configuration of my http provider. All my controllers have to use the same configuration of the http provider in app.js .

In app.js,

angular.module('myApp', [
  'ngRoute',
  'myApp.filters',
  'myApp.services',
  'myApp.directives',  
  'myApp.controllers',
]).
config(['$httpProvider', function($httpProvider)
{
    if (!$httpProvider.defaults.headers.get) {
        $httpProvider.defaults.headers.get = {};    
    }

    //disable IE ajax request caching
    $httpProvider.defaults.headers.get['If-Modified-Since'] = '0'; 
}

I have a controller XXXCtrl which works when app.js is as above. I have another controller YYYCtrl which does not work unless I comment away the code inside config(['$httpProvider', function($httpProvider).

So, my problem is this. How to get different controllers to use different configurations of $httpProvider in AngularJS? Can $httpProvider be configured inside individual controllers?


Solution

  • You can override $http headers per call

    $http.get(url, {headers:{...}});
    

    Since services are singletons, they are configured once and for all.You can have multiple module.config functions but it wouldnt help.

    You could also use Interceptors to configure http requests.