AngularJS - 将工厂从另一个模块注入提供者 [英] AngularJS - Injecting factory from another module into a provider

查看:25
本文介绍了AngularJS - 将工厂从另一个模块注入提供者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个来自单独模块的工厂,我想将其注入到我的模块的提供程序中,但我不断收到未知的提供程序错误.我做错了什么?

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?

我想注入什么:

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;
    }
]);

我尝试注入的地方:

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);
        };
    }]);

结果是

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

推荐答案

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

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

作为一种解决方法,我使用 angular.moduleinjector 方法来创建模块.一个应该做你想要完成的事情的plunker:http://plnkr.co/edit/g1M7BIKJkjSx55gAnuD2

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"]);

这篇关于AngularJS - 将工厂从另一个模块注入提供者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆