javascript - Declare AngularJS service multiple times in the same module -


i working angularjs 1.x , can define same service multiple times in same module. in following snippet of code, defining service nameservice 2 times in module myapp:

(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 use value returned second implementation of service: "second definition". please check this plunker example.

so, empirically, angularjs seems use latest definition of service, ignoring previous ones.

my question is: there official documentation describing behavior?

here, overwriting definition of factory/service. factory/service singleton i.e. there 1 instance of it.

factory('name', constructing_function) 

your case,

factory('nameservice', nameservice1)//here object { getname: getname } assigned nameservice instance.  factory('nameservice', nameservice2)//here object { getname: getname } (#2nd definition) assigned nameservice instance. i.e. nameservice instance referring (#2nd definition). 

Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -