How to inject dependencies into a provider using Angularjs?

前端 未结 5 1683
谎友^
谎友^ 2020-12-29 03:00

Is it possible to do DI in a provider method?

In this example

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

  this.$get=function()         


        
相关标签:
5条回答
  • 2020-12-29 03:07

    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

    0 讨论(0)
  • 2020-12-29 03:12

    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.

    0 讨论(0)
  • 2020-12-29 03:14

    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) {
    
      };
    });
    
    0 讨论(0)
  • 2020-12-29 03:23

    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...

    0 讨论(0)
  • 2020-12-29 03:24

    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
        };
    });
    
    0 讨论(0)
提交回复
热议问题