18

I have a factory from a separate module that I would like to inject into a provider for my module, but I keep getting unknown provider errors. What am I doing wrong?

What I would like to inject:

var angularSocketIO = angular.module('socketioModule', []);
angularSocketIO.factory('socketio', [
    '$rootScope', 
    'addr', 
    function($rootScope, addr) {
        var socket = io.connect(addr,{
            'sync disconnect on unload': true
        });
                ...
        return socket;
    }
]);

Where I am trying to inject it:

angular.module('myApp.services', ['socketioModule'])
    .provider('greeter', ['socketio', function(socket) {
        var salutation = 'Hello';
        this.setSalutation = function(s) {
            salutation = s;
        }

        function Greeter(a) {
            this.salutation = salutation;
            socket._emit('hello')

            this.greet = function() {
                return salutation + ' ' + a;
            }
        }

        this.$get = function(version) {
            return new Greeter(version);
        };
    }]);

That results in

Error: [$injector:modulerr] Failed to instantiate module myApp due to:
[$injector:modulerr] Failed to instantiate module myApp.services due to: 
[$injector:unpr] Unknown provider: socketio
aron.duby
  • 2,072
  • 2
  • 15
  • 21

2 Answers2

22

I think is because all the providers are instantiated before the factories and so a provider has to depend only on other providers.

As a way around that, I am using the injector method of angular.module to create the module. A plunker that should do what you were trying to accomplish: http://plnkr.co/edit/g1M7BIKJkjSx55gAnuD2

Notice that I changed also the factory method. The factory method is now returning an object with a connect method.

var angularSocketIO = angular.module('socketioModule', ['ng']);
angularSocketIO.factory('socketio', [
    '$rootScope',
    function($rootScope) {
      return {
        connect: function(addr) {
          var socket = io.connect(addr, {
            'sync disconnect on unload': true
          });

          return socket;
        }
      };
    }]);


  angular.module('myApp.services', ['socketioModule'])
  .provider('greeter', [
    function() {
      var injector = angular.injector(['socketioModule']);
      var socketio = injector.get('socketio');

      var salutation = 'Hello';
      this.setSalutation = function(s) {
        salutation = s;
      }

      function Greeter(a) {
        this.salutation = salutation;
        socket._emit('hello');

        this.greet = function() {
          return salutation + ' ' + a;
        };
      }

      this.$get = function(version) {
        return new Greeter(version);
      };
    }
  ]);


  var myApp = angular.module('myApp', ["myApp.services"]);
fabrizioM
  • 46,639
  • 15
  • 102
  • 119
  • That did the trick, but only when I included the ng in the socket module. What is that portion doing? Also, if my factory were switched to a provider do you think that would fix it as well? I was thinking I might switch that to get the config ability. Thanks for the help! – aron.duby Nov 01 '13 at 15:07
  • I had the same issue, and ended up using the injector...is there a best practice here? In my case, I was injecting factory that wrapped lodash into a provider called "config" (wanted to use lodash's _extend b/c angular's 'extend' sucks). Didn't seem to make sense stylistically to make that factory a provider just so i could inject it into another provider. But curious as to what the "best" way to do it would have been... – herringtown Nov 23 '13 at 17:31
  • 1
    Hmm, what would be the best practice though? I did the same thing and I'm feeling a bit dirty to have done that :)) – Arthur Kovacs Mar 26 '14 at 21:12
  • "...a provider has to depend only on other providers.". I needed a provider to depend on another provider - in order to get this to work, the app must declare the depended provider first (basically, order of declaration matters in this case). – aaaaaa Jan 09 '15 at 23:34
  • @herringtown Since lodash is available before angular bootstrap phase, just use `.constant('lodash', _)` – felixfbecker Aug 11 '15 at 13:17
  • Is this working for you guys in tests? Because for me it causes phantom crash. – masterspambot Dec 20 '15 at 19:39
10

I think you can add dependencies via $get method in provider:

angular.module('myApp.services', ['socketioModule'])
  .provider('greeter', [
    function() {
        ...
        this.$get = ['socketio', function(socket, version) {
            function Greeter(a) {
                  this.salutation = salutation;
                  socket._emit('hello')

                  this.greet = function() {
                      return salutation + ' ' + a;
                  }
             }
             return new Greeter(version);
      }];
   }  
]);
huan feng
  • 7,307
  • 2
  • 32
  • 56