I have an app module which include another modules for example module a and b. But both module a and b depends on module c. See the followings
var app = angular.module('app', ['a', 'b']);
app.controller('AppCtrl', function(Containner) {
console.log(Containner); //Avaiable
});
var a = angular.module('a', ['c']);
a.controller('ACtrl', function(Containner) {
console.log(Containner); //Avaiable
});
var b = angular.module('b', ['c']);
b.controller('BCtrl', function(Containner) {
console.log(Containner); //Avaiable
});
var c = angular.module('c', []);
c.factory('Containner', function() {
return {
test: 'test'
}
});
From what I understand, you only need to include a module once and then you can access it in other module even you do not include it as its dependency. See the followings
var app = angular.module('app', ['a', 'b']);
app.controller('AppCtrl', function(Containner) {
console.log(Containner);
});
var a = angular.module('a',[]);
a.controller('ACtrl', function(Containner) {
console.log(Containner); //YOU CAN STILL GET IT
});
var b = angular.module('b', ['c']);
b.controller('BCtrl', function(Containner) {
console.log(Containner);
});
var c = angular.module('c', []);
c.factory('Containner', function() {
return {
test: 'test'
}
});
Module A does not has C as its dependency, but it still can access C's service. That is, I assume as long as you include C in one place, you can use in any other modules as long as somewhat they are all included in the angular application.
I prefer the first piece of code since each module clearly inform developers its own dependency. However, it seems redundant since you just need to include once and it will be available in other place. What is the best practise for this situation and how angular handles the duplicated dependency behind the scenes.
app.js
then it will be always available for complete project. and if you added dependency in only one file then you have to add it every time you need because it will be available only for that particular file, So the good practice is always to add it in application main file i.eapp.js
.AND in your scenario adda
,b
,c
at one place onlyapp.js
so you can use all dependency in each other