Jivani Jaydeep Jivani Jaydeep - 16 days ago 7
AngularJS Question

Get module name and its all dependencies list in angularjs

I want to get

module name
and
all dependencies list
that is
injected
in
application module
.

Like :

var app = angular.module( 'wp', ['ngRoute', 'abc', 'xyz'] );


So from this i want to get all this component list like
wp,ngRoute,abc,xyz


Using
Angular JS.


Thanks

Answer

In angular js, you can use the requires array to get all the dependency modules of the main module.

The syntax of the module declaration:

angular.module(name, [requires], [configFn]);

The second parameter, the dependencies in an array named requires.

So, app.requires gives you the required result.

Here is an example:

var app = angular.module('App', [
    'ngRoute',
    'appServices',
    'toaster',
    'ngValidate',
    'uiGmapgoogle-maps',
    'ngFileUpload',
    'offClick'
]);

console.log(app.requires);

gives you,

Array[8] 

[
"ngRoute",
"appServices",
"toaster",
"ngValidate",
"uiGmapgoogle-maps",
"ngSanitize",
"ngFileUpload",
"offClick"
]

Update:

1) If you don't have a variable for your module, you can use,

angular.module('App').requires

2) If you don't know the module name at all, then you can use,

angular.module($rootElement.attr('ng-app')).requires;

for this you should add $rootElement as dependency

Eg:

.run(["$rootScope","$rootElement",function($rootScope,$rootElement){

   console.log(angular.module('App').requires);

   console.log(angular.module($rootElement.attr('ng-app')).requires);

}]);

3) Even if you dont have the code with you you can simply try, $("[ng-app]").attr('ng-app') if you have jquery to get the module name.

angular.module($("[ng-app]").attr('ng-app')).requires;