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

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

问题描述

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



我想注入什么:

  var angularSocketIO = angular.module('socketioModule',[]); 
angularSocketIO.factory('socketio',[
'$ rootScope',
'addr',
函数($ 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;
}
}

这个$ get = function(version){
return new Greeter(version);
};
}] );

这导致

 错误:[$ inject:modulerr]由于:
[$ inject:modulerr]无法实例化模块myApp由于以下原因,未能实例化模块myApp.services:
[$ inject: unr]未知提供者:socketio


解决方案

我认为是因为全部提供商在工厂之前实例化,因此提供商只能依赖于其他提供商。



作为一种方式,我使用注入方法 angular.module 来创建模块。
应该做你想要完成的一个洞穴: http://plnkr.co/edit/g1M7BIKJkjSx55gAnuD2



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

  var angularSocketIO = angular.module('socketioModule ',['ng']); 
angularSocketIO.factory('socketio',[
'$ rootScope',
函数($ 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;
};
}

这个$ get = function(version){
return new Greeter(version);
};
}
]);


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天全站免登陆