5

I'd like to do somtehing like that:

angular.module('app', []).config(
  [ '$httpProvider', 'customAuthService',
    ($httpProvider, customAuthService) ->
      $httpProvider.defaults.transformRequest.push (data) ->
        if customAuthService.isLoggedIn
          data['api_key'] = {token: @token}
  ])

According to Angularjs doc, I can't do it in the config block of my module, because custom services are not allowed there, nor can I do it in the run block, because providers like $httpProvider aren't allowed there:

Configuration blocks - get executed during the provider registrations and configuration phase. Only providers and constants can be injected into configuration blocks. This is to prevent accidental instantiation of services before they have been fully configured.

Run blocks - get executed after the injector is created and are used to kickstart the application. Only instances and constants can be injected into run blocks. This is to prevent further system configuration during application run time.

How can I do to add some configuration in my $httpProvider that relies on a home-made service ?

Community
  • 1
  • 1
ejoubaud
  • 5,013
  • 6
  • 37
  • 39

2 Answers2

7

It is always possible to get an injector and then the instance of the service inside the callback function ('service locator' style as opposed to having the dependency injected in the config function).

I guess is ok for exceptional cases, although not pretty to use it extensively.

.config([ '$httpProvider', function($httpProvider)  {
    $httpProvider.defaults.transformRequest.push(function(data) {

        var $injector = angular.injector(['app']);
        var customAuthService = $injector.get('customAuthService');

        // ...
      });
  }])

But, instead of doing that...

Have you looked at Response interceptors in the $http documentation?

It looks better suited for authentication purpouses and you can get the service injected there.

garst
  • 6,042
  • 1
  • 29
  • 24
  • 1
    I did end up configuring `$http` instead of `$httpProvider`, which provides exactly the same value, as far as my use case is concerned. Thanks. – ejoubaud Jul 24 '13 at 22:22
  • Any way of using `angular.injector` and getting the same service instance that will be used by the module you are calling the `config(...)` method? – Felipe Sabino Aug 08 '13 at 20:30
0

You can inject it to functions inside of your config as far as I know. I use something similar to intercept requests if they aren't logged in using my auth service.

.config(['$httpProvider',function ($httpProvider) {
    var authRequest= ['customAuthService', function(customAuthService) {
       if(customAuthService.isLoggedIn){
           data['api_key'] = {token: @token};
       }  
    }];
    $httpProvider.defaults.transformRequest.push(authRequest);
}]);
Tim Withers
  • 12,072
  • 5
  • 43
  • 67
  • Doesn't work, the method that reads request transformers is expecting callbacks, not arrays, so I get a `TypeError: object is not a function`. – ejoubaud May 07 '13 at 05:12