I was working on an AngularJS web application which had quite a few sub-modules. Two of the sub-modules had the CRUD functionality therefore both these modules had a few controllers with the same name. For an idea of how the code looked like, have a look at the snippet below:
<!DOCTYPE html>
<html>
<head>
<script data-require="[email protected]" data-semver="1.5.6" src="https://code.angularjs.org/1.5.6/angular.min.js"></script>
<link rel="stylesheet" href="style.css" />
<script>
angular
.module('app', [
'app.one',
'app.two'
]);
angular
.module('app.one', [])
.controller('MyCtrl', function() {
var vm = this;
vm.message = 'Hello World from app.one!';
});
angular
.module('app.two', [])
.controller('MyCtrl', function() {
var vm = this;
vm.message = 'Hello World from app.two!';
});
</script>
</head>
<body ng-app="app" ng-controller="MyCtrl as vm">
<h1>{{ vm.message }}</h1>
</body>
</html>
You can also find this at plnkr: http://plnkr.co/edit/JRwJsrJd84nPnjj36GAZ.
Now, the problem is that I'm confused. To me it doesn't make sense for AngularJS to confuse controllers of same names but in different modules. If anyone can explain this and also suggest a way to overcome this, I'd be really appreciate that.
Thanks in advance!
requiresfor modules are loaded one by one, consolidating each$providevalues in$injectoror parent module. Angular.js:function module(name, requires, configFn) { ... },function loadModules(modulesToLoad) { ... }. From notes$injectoris bind to element, so if you haveappand all$providedis bind toappelement, you just overwroteMyCtrlwhen you$injectedapp.two.$injectand$provideworks with names as keys, that's why you overwrote it.