Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJs - get list of all registered modules

Tags:

angularjs

Can I get a list of all registered modules at run time?

For example:

// Some code somewhere in some .js file
var module1 = angular.module('module1', []);

// Some code in some other .js file
var module2 = angular.module('module2', []);

// Main .js file
var arrayWithNamesOfAllRegisteredModules = .....

// (result would be: ['module1', 'module2'])
like image 977
user1147862 Avatar asked Jul 22 '14 14:07

user1147862


People also ask

How to retrieve the list of registered modules in angular?

Angular does not provide a way to retrieve the list of registered modules (at least I was not able to find a way in source code). You can however decorate angular.modulemethod to store names in array. Something like this:

What is the angular module used for?

The angular.moduleis a global place for creating, registering and retrieving AngularJS modules. All modules (AngularJS core or 3rd party) that should be available to an application must be registered using this mechanism. Passing one argument retrieves an existing angular.Module,

How do I register an injectable in angular?

Registration in the config block. While it is recommended to register injectables directly with the module API, it is also possible to register services, directives etc. by injecting $provide or the individual service providers into the config function: angular.module('myModule', []).

What is the main method of an AngularJS application?

AngularJS apps don't have a main method. Instead modules declaratively specify how an application should be bootstrapped. There are several advantages to this approach: The declarative process is easier to understand. You can package code as reusable modules.


3 Answers

Angular does not provide a way to retrieve the list of registered modules (at least I was not able to find a way in source code). You can however decorate angular.module method to store names in array. Something like this:

(function(orig) {
    angular.modules = [];
    angular.module = function() {
        if (arguments.length > 1) {
            angular.modules.push(arguments[0]);
        }
        return orig.apply(null, arguments);
    }
})(angular.module);

Now you can check angular.modules array.

Demo: http://plnkr.co/edit/bNUP39cbFqNLbXyRqMex?p=preview

like image 131
dfsq Avatar answered Oct 17 '22 02:10

dfsq


You can simply do :

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

It should return of an array of strings (dependencies). You can do the same for the output.

like image 22
Yacine Avatar answered Oct 17 '22 04:10

Yacine


Given an angular.element, the $injector.modules array contains the list of registered modules.

e.g.

angular.element(document.body).injector().modules
like image 45
7uc4 Avatar answered Oct 17 '22 04:10

7uc4