1

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.

2
  • If you include dependancy in 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.e app.js.AND in your scenario add a, b, c at one place only app.js so you can use all dependency in each other Commented Oct 19, 2016 at 18:10
  • @ojuskulkarni I would say the best practice is for a module (including main app) to always and only include modules it uses directly. That is in this case, if app module uses a and b but not c, it should list a and b but not c as its dependencies. If module a uses module c, it should list module c, etc. When building modularly, you must think modularly and making your modules portable and reusable. Commented Oct 19, 2016 at 18:15

1 Answer 1

1

The first way is absolutely better for a couple reasons.

First of all, it is very clear and doesn't rely on any mysterious, uncommunicated expectations that another module is including its dependencies for it.

Second, with the intention of reusability being a core aspect of modular design, it would be necessary if you want to use the module in an app where module 'b' doesn't exist.

I would say the best practice is for a module to always and only list the modules it uses directly.

If app uses a and b but not c then it should list its dependences as ['a', 'b'] but not c.

If a uses c then it should list its dependencies as ['c']

Code should be written to be as clear as possible, and leave a programmer asking as few questions as possible. (I really don't want to wonder why module c is not being included if it is being referenced)

Sign up to request clarification or add additional context in comments.

1 Comment

Also note the second example shows a flaw in angular's design, and is not a "feature" in any way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.