简体   繁体   English

在同一模块中多次声明AngularJS服务

[英]Declare AngularJS service multiple times in the same module

I am working with AngularJS 1.x and I can define the same service multiple times in the same module. 我正在使用AngularJS 1.x,我可以在同一个模块中多次定义相同的服务。 In the following snippet of code, I am defining the service nameService 2 times in module myApp : 在下面的代码片段中,我在模块myApp定义服务nameService 2次:

(function() {
  'use strict';

  angular
    .module('myApp', [])
    .controller('MainController', MainController)
    .factory('nameService', nameService1)
    .factory('nameService', nameService2);

  MainController.$inject = ['nameService'];

  function MainController(nameService) {
    var vm = this;

    vm.message = nameService.getName();
  }

  function nameService1() {
    return {
      getName: getName
    };

    function getName() {
      return 'First Definition';
    }
  }

  function nameService2() {
    return {
      getName: getName
    };

    function getName() {
      return 'Second Definition';
    }
  }
})();

At runtime, AngularJS will use the value returned by the second implementation of the service: "Second Definition". 在运行时,AngularJS将使用服务的第二个实现返回的值:“Second Definition”。 Please check the this Plunker example . 请查看此Plunker示例

So, empirically, AngularJS seems to use always the latest definition of a service, ignoring the previous ones. 因此,根据经验,AngularJS似乎始终使用服务的最新定义,而忽略了之前的定义。

My question is: is there any official documentation describing this behavior? 我的问题是:有没有描述这种行为的官方文件?

Here, you are overwriting definition of factory/service. 在这里,您将覆盖工厂/服务的定义。 factory/service is singleton ie there will be only one instance of it. 工厂/服务是单身,即只有一个实例。

factory('name', constructing_function)

Your case, 你的情况,

factory('nameService', nameService1)//Here object { getName: getName } will be assigned to nameService instance.

factory('nameService', nameService2)//Here object { getName: getName } (#2nd definition) will be assigned to nameService instance. i.e. nameservice instance referring to (#2nd definition).

That's the way how JavaScript works: 这就是JavaScript的工作方式:

function Vinoth(){console.log("Printing first")}

function Something(){console.log("Printing Something")}

function Vinoth(){console.log("Printing Second")}

When you do a console.log(Vinoth()); 当你做一个console.log(Vinoth()); , it would always print the Second one. ,它总是打印Second个。 Over to your second part: 到你的第二部分:

  angular
    .module('myApp', [])
    .controller('MainController', MainController)
    .factory('nameService', nameService1)
    .factory('nameService', nameService2);

AngularJS services are singleton by nature, so its going to be one instance across your application life cycle. AngularJS服务本质上是singleton的,因此它将成为整个应用程序生命周期中的一个实例。 Your nameService1 and nameService2 are pointing to same nameService instance . 您的nameService1 and nameService2指向同一个nameService instance Technically it nameService holds nameService2 as it precedes according to hoisting. 从技术上讲, nameService根据提升而保持nameService2

No there is no official documentation for this function. 没有关于此功能的官方文档。

If you would like there to be you can ask the maintainers of angularjs to add documentation for this feature. 如果您愿意,可以请求angularjs的维护者为此功能添加文档。

Don't need to define the service (Inject) two times. 不需要定义服务(Inject)两次。 Better solution is , you define the service one time. 更好的解决方案是,您可以定义一次服务。 And use like, 并使用像,

angular.module('myApp', []).controller('MainController', MainController) .factory('nameService', nameService1)

Inside this , we can use that service like, 在这里,我们可以使用那样的服务,

var nameService2 = angular.copy(nameService);

This is for best practice. 这是最佳做法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM