AngularJS - 将工厂从另一个模块注入提供者

aro*_*uby 18 dependency-injection angularjs

我有一个单独的模块工厂,我想注入我的模块的提供程序,但我一直得到未知的提供程序错误.我究竟做错了什么?

我想注入什么:

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;
    }
]);
Run Code Online (Sandbox Code Playgroud)

我试图注入的地方:

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);
        };
    }]);
Run Code Online (Sandbox Code Playgroud)

这导致了

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
Run Code Online (Sandbox Code Playgroud)

fab*_*ioM 22

我认为是因为所有提供程序都在工厂之前实例化,因此提供程序必须仅依赖于其他提供程序.

作为解决injector方法,我正在使用angular.module创建模块的方法.一个应该做你想要完成的东西:http ://plnkr.co/edit/g1M7BIKJkjSx55gAnuD2

请注意,我也更改了工厂方法.工厂方法现在使用connect方法返回一个对象.

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"]);
Run Code Online (Sandbox Code Playgroud)


hua*_*eng 10

我认为您可以通过提供程序中的$ get方法添加依赖项:

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);
      }];
   }  
]);
Run Code Online (Sandbox Code Playgroud)