【问题标题】:How can I inject a service into another service with specific function names如何将服务注入具有特定功能名称的另一个服务
【发布时间】:2015-12-15 11:59:50
【问题描述】:

假设我的代码如下

var app = angular.module("myApp", []);
app.controller("Customerfactory", Customerfactory); 

app.service("Address",Address);
app.service("Phone",Phone);

app.service("customer",customer);
app.service('CreateFactory',CreateFactory);

我想使用数组注入技术将AddressPhone 服务注入customer 服务,然后将customer 服务注入createFactory 服务。

任何想法。

【问题讨论】:

标签: angularjs


【解决方案1】:

我没有使用任何方括号在 Angular 中声明,减少错误

app.service("customer", function(Address, Phone){
});

app.service('CreateFactory', function(customer){
});

【讨论】:

    【解决方案2】:

    您只需使用 Angular 中的常规注入规则(遵循能够缩小代码的约定):

    app.service('service2',['service1', function(service1) {}]);
    

    【讨论】:

      【解决方案3】:

      这样试试

      app.service("customer",['Address','Phone',function(Address,Phone){
      
      
      }]);
      
      app.service('CreateFactory',['customer',function(customer){
      
      }]);
      

      【讨论】:

      • 谢谢@Anik 如果我调用一个由它定义的函数会怎样。 'app.service('CreateFactory',['customer',CreateFactory(customer)]);'这可以接受吗?如果是这样,我如何访问在 CreateFactory 服务的 Address 服务中定义的变量。我还在学习 AngularJs,如果我有任何错误,请见谅。
      猜你喜欢
      • 2013-12-22
      • 1970-01-01
      • 2020-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-27
      相关资源
      最近更新 更多