1

Is it possible to from the global window or window.angular object loop through all controllers/modules/factories etc. that we have created and is being bootstrapped?

I'm thinking something like:

for(module in window.angular.modules) {
  for(ctrl in module.controllers) {
    console.log(ctrl);
  }
  for(factory in module.factories) {
    console.log(factory);
  }
  ...
}

Goal: I want to auto-generate some documentation for the app we've created.

Edit: Note that we are not creating global objects when doing controllers. We are registring them directly on the module:

angular.module('ourApp')
  .controller('CustomerCtrl', ['$scope', function ($scope) { ... } ]);
Cotten
  • 8,787
  • 17
  • 61
  • 98
  • Try also to take a look at http://stackoverflow.com/questions/15250644/angularjs-loading-a-controller-dynamically – Whisher Feb 12 '14 at 10:02

2 Answers2

2

The angular module contains an attribute called _invokeQueue, that contains an array of all the submodules that are part of this module.

console.log(angular.module('ourApp')._invokeQueue);

prints something like

[
    ['$provide', 'factory', Arguments['myFactory', ['$dependency1', '$dependency2', function(){}],
    ['$provide', 'service', Arguments['myService', ['$dependency1', '$dependency2', function(){}]
    ['$provide', 'constant', Arguments['myConstant', ['$dependency1', '$dependency2', function(){}]
    ['$controllerProvider', 'register', Arguments['myController', ['$dependency1', '$dependency2', function(){}]
    ...
]

Note: If you're building a documentation generation app, then in my opinion you shouldn't loop all registered modules, since there are many built-in angular modules (ng, ngRoute, etc) that you probably don't care about. You should instead specify which modules you want to document.

Alon Gubkin
  • 56,458
  • 54
  • 195
  • 288
  • console.log(angular.module('ourApp')._invokeQueue); I get only the current module not all the parent. BTW is there a way to get the module name dynamically @Cotten sorry :) – Whisher Feb 12 '14 at 09:02
0

Run in browser console:

To get list of registered controllers from angular application

angular.module('appname')._invokeQueue.filter(function(comp){return comp[0] === '$controllerProvider'}).forEach(function(ctrl){console.log(ctrl[2][0]);})

To get list of registered providers from angular application

angular.module('appname')._invokeQueue.filter(function(comp){return comp[0] === '$provide'}).forEach(function(ctrl){console.log(ctrl[2][0]);})