user1147862 user1147862 - 1 month ago 13
AngularJS Question

AngularJs - get list of all registered modules

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'])

Answer

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

Comments