Chung
Chung

Reputation: 1063

How to inject dependencies into a provider using Angularjs?

Is it possible to do DI in a provider method?

In this example

angular.module('greet',[])
.provider('greeter',function() {

  this.$get=function() {

  };
})
.service('greeterService',function($http){
  console.log($http);
})
;

Injecting $http into service appears to be the correct implementation, but it doesn't work in a provider method and it throws an error:

Unknown provider: $http

Does the provider method work with DI to inject services?

Upvotes: 33

Views: 33020

Answers (5)

Dunc
Dunc

Reputation: 18922

Following up on IgrCndd's answer, here's a pattern that might avoid potential nastiness:

angular.module('greet',[]).provider('greeter', function() {

    var $http;

    function logIt() {
        console.log($http);
    }

    this.$get = ['$http', function(_$http_) {
        $http = _$http_;

        return {
            logIt: logIt
        };
    }];
});

Note how similar this is to the equivalent service, making conversion between the two less troublesome:

angular.module('greet',[]).factory('greeter', ['$http', function($http) {

    function logIt() {
        console.log($http);
    }

    return {
        logIt: logIt
    };
});

Upvotes: 7

z0r
z0r

Reputation: 8585

You can inject constants and other providers into a provider. Not services or factories - with one exception. It seems that you can inject the $injector service into a provider - at least, you can in AngularJS 1.3.16.

.provider('foo', ['$injector', function ($injector) {
  var messagePrefix = $injector.get('msgPrefix');
  this.message = '';

  this.$get = function() {
    var that = this;
    return function() {
      return messagePrefix + that.message;
    }
  };
}])

You can use the injector outside the $get method, but you still can't get services from it at configure time.

See here for a demo.

Upvotes: 13

Jens
Jens

Reputation: 5877

No, you can not inject a service into the provider itself. Injecting a service into a provider's $get method is the same as injecting a service into a factory, but you can not inject it into the provider function directly.

The difference between $get and the provider itself is that the provider runs during the module loading phase whereas the $get is run when instantiating the service you are providing.

This implies that you can not use any service at all during the module loading/configuration phase of your modules. That is all the stuff you run inside your config blocks, such as when defining your app routes or states, can not make use of any service.

The only other thing you can inject into config blocks besides providers are constants.

You could do something like IgrCndd suggested. But if you needed to consume the provider in a config block, which is the provider's purpose after all, you will not have your values injected until much after. So it's not going to work unless you do some nasty hack using promises.

Further reading on injectables

Upvotes: 3

IgrCndd
IgrCndd

Reputation: 53

You actually have to inject the dependency on $get and then store it to use on what you retrieve from $get. Not beautiful at all...

Upvotes: 3

Buu
Buu

Reputation: 50345

You can certainly inject $http to provider. Just make sure it appears in $get, not the function constructor. As follows:

angular.module('greet',[]).provider('greeter',function() {
  this.$get = function($http) {

  };
});

Upvotes: 60

Related Questions