I am working with AngularJS 1.x and I can define the same service multiple times in the same module. In the following snippet of code, I am defining the 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 will use the value returned by the second implementation of the service: "Second Definition". Please check the this Plunker example.
So, empirically, AngularJS seems to use always the latest definition of a service, ignoring the previous ones.
My question is: is there any official documentation describing this behavior?