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

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

问题描述

我有一个单独的模块,我想注入我的模块供应商工厂,但我不断收到未知的提供程序错误。我在做什么错了?

我想什么来注入:

  VAR angularSocketIO = angular.module('socketioModule',[]);
angularSocketIO.factory('socketio',[
    '$ rootScope',
    '地址',
    功能($ rootScope,地址){
        VAR插座= io.connect(地址,{
            上卸载同步断开连接:真实
        });
                ...
        返回插座;
    }
]);

在哪里我试图注入它:

  angular.module('myApp.services',['socketioModule'])
    .provider('迎宾',['socketio',函数(插座){
        变种称呼=你好;
        this.setSalutation =函数(多个){
            称呼= S;
        }        功能迎宾(一){
            this.salutation =称呼;
            socket._emit('你好')            this.greet =功能(){
                返回称呼+''+ A;
            }
        }        这一点。$ GET =功能(版本){
            返回新迎宾(版本);
        };
    }]);

这导致

 错误:[$喷油器:modulerr]未能实例化模块对myApp由于:
[$喷油器:modulerr]未能实例由于模块myApp.services:
[$喷油器:unpr]未知提供商:socketio


解决方案

我想是因为所有的供应商正在工厂之前实例化,所以提供者只在其他供应商的依赖。

由于办法解决,我现在用的注射器 angular.module 的方法来创建模块。
应该做一个什么plunker你试图完成: http://plnkr.co/edit/g1M7BIKJkjSx55gAnuD2

请注意,我也改变了工厂方法。工厂方法现在返回一个对象
用连接方法。

  VAR angularSocketIO = angular.module('socketioModule',['NG']);
angularSocketIO.factory('socketio',[
    '$ rootScope',
    功能($ rootScope){
      返回{
        连接:功能(地址){
          VAR插座= io.connect(地址,{
            上卸载同步断开连接:真实
          });          返回插座;
        }
      };
    }]);
  angular.module('myApp.services',['socketioModule'])
  .provider('迎宾',[
    功能(){
      变种注射器= angular.injector(['socketioModule']);
      变种socketio = injector.get('socketio');      变种称呼=你好;
      this.setSalutation =函数(多个){
        称呼= S;
      }      功能迎宾(一){
        this.salutation =称呼;
        socket._emit('你好');        this.greet =功能(){
          返回称呼+''+ A;
        };
      }      这一点。$ GET =功能(版本){
        返回新迎宾(版本);
      };
    }
  ]);
  VAR对myApp = angular.module('对myApp',[myApp.services]);

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

解决方案

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

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

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