javascript - 为什么我能够在不注入(inject)模块的情况下访问 Angular 服务?

标签 javascript angularjs dependency-injection

我正在玩angular services并发现我能够使用 Angular 服务,甚至无需注入(inject)它的模块。我创建了 3 个模块,即 moduleA、moduleB、appmoduleAmoduleB 彼此独立,但 app 模块依赖于 moduleAmoduleB >。这就是我创建模块的方式。

angular.module('moduleA',[]);
angular.module('moduleB',[]);
angular.module('app',['moduleA', 'moduleB']); 

moduleA中,我有一个服务ServiceA,在moduleB中,我有一个服务ServiceB。这就是我定义服务的方式:

模块A <-- 服务A:

(function() {
  var app = angular.module('moduleA', []);
  app.service('ServiceA', function() {
    'use strict';
    this.greet = function() {
      alert('Greetings From Service A');
    };
  });
}());

moduleB <-- ServiceB: 我已在 serviceB 中注入(inject)了 serviceA

(function() {
  'use strict';
  var app = angular.module('moduleB', []);
  app.service('ServiceB', function(ServiceA) {
    // I have not injected moduleA in moduleB but still
    // Don't know why I am able to access ServiceA
    // in ServiceB. Ideally I should not be able to access
    // anything which is defined in moudleA unlesss and until
    // I do not inject moduleA in moudleB.
    console.log(ServiceA);
    this.greet = function() {
      ServiceA.greet();
    };
  });
}());

最后 应用程序模块:

(function() {
  'use strict';
  var app = angular.module('app', ['moduleA', 'moduleB']);
  app.controller('MainController', ['$scope', 'ServiceA', 'ServiceB', function($scope, ServiceA, ServiceB) {
    $scope.greet = function() {
      ServiceB.greet();
    };
  }])
}());

现在担心的是我没有在moduleB中注入(inject)moduleA,但我仍然能够访问ServiceA moduleBServiceB 中的 >moduleA 我不知道为什么会发生这种情况。我对模块的理解是它们类似于Java或Dot Net的packagenamespace。我们在模块中创建的任何内容都位于该特定模块中。为了获取该模块中定义的服务或特殊对象,我必须注入(inject)该模块。但不知道为什么我什至不用注入(inject)就能访问它。这是fiddle玩。

最佳答案

加载模块后,其服务即可用于注入(inject)。一旦在当前$injector上定义了服务提供者,它就不会保存它属于哪个模块的信息。

上面的代码是可行的,但它指定了一个坏习惯,因为如果 moduleA 未加载,它将无法工作。根据经验,模块应该加载它们所依赖的模块。在多个依赖模块中加载一个模块是可以接受的,它只会被加载一次。

使用 config block 中的提供程序注入(inject)器,事情变得更加复杂(有 two separate $injectors 用于服务提供程序和服务实例)。

对于此装载订单

angular.module('app',['moduleA', 'moduleB']); 

这将按预期工作,因为 ServiceA 服务提供者在 config block 运行时已定义:

angular.module('moduleB', []).config(function (ServiceAProvider) { ... });

对于这个加载顺序

angular.module('app',['moduleB', 'moduleA']); 

将引发注入(inject)器错误,因为 config block 在 app.service('ServiceA', ...) 定义 ServiceAProvider 服务之前运行提供者。 app.service 方法可能会早于 config 运行,但服务定义会排队并且不会立即生效。

关于javascript - 为什么我能够在不注入(inject)模块的情况下访问 Angular 服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37861288/

相关文章:

javascript - Sprite 动画: function not recognized and semi-colon expected in for-loop

javascript - 如何在刷新网页时设置不可启动计数器?

javascript - nodejs模块之间的调用函数

javascript - 语法错误 : Unexpected token o in JSON at position 1

ruby-on-rails - Heroku + Rails + Angular = 未知提供商 : eProvider <- e

javascript - 如何使用父数组和嵌套数组进行 _.flatten ? (洛达什)

c# - Autofac IComponentContext.Resolve<Type> 是服务定位器模式吗

java - 在普通 Java EE 中使用工厂方法定义 bean - 无需 Spring

c# - 重新设置单例httpclient的证书(Reconfiguring IHttpclientFactory?)

html - AngularJS 中的 md-content 滚动问题