Diosney
Diosney

Reputation: 10580

How get a list of dependent modules in within some parent module?

Setup:

Several angularjs modules declared as:

angular.module('a', ['a.b']);

angular.module('a.b', ['a.b.c1', 'a.b.c2']);

angular.module('a.b.c1', []);

angular.module('a.b.c2', []);

Question:

Is there a way to get all children modules from a specific parent module? I mean something like:

angular.module.getChildrenFromParent('a.b');

That should return an array of strings or an array of references to the modules a.b.c1 and a.b.c2.

Thanks

UPDATE

I know there is How can I get a list of available modules in AngularJS? but it isn't a clean solution and it is a bit of an old answer too.

Upvotes: 1

Views: 147

Answers (1)

PSL
PSL

Reputation: 123739

You can use the requires property of the module object returned (by angular.module getter invocation) to get the dependency list.

i.e

var dependentModules = angular.module('a.b').requires;

This will output an array of strings representing the dependencies ["a.b.c1", "a.b.c2"]. But note that you can use it only after module has been created otherwise invoking this for a module that has not been created will result in an error. And if you want to go deep just implement a recursive lookup for each dependencies.

Demo

angular.module('a.b.c1', []);

angular.module('a.b.c2', []);
angular.module('a.b', ['a.b.c1', 'a.b.c2']);

angular.module('a', ['a.b']);

console.log(angular.module('a.b').requires);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

Upvotes: 2

Related Questions