Is it possible to get a list of all the Angular controllers that I have defined? Essentially I want to be able to determine which files
I need to import (ones I wrote) depending on which ones are used. The only way I can think of is to traverse through the HTML and find all the values associated with ng-controller
, but I was wondering if there was a cleaner, more robust way.
Asked
Active
Viewed 5,653 times
13

Danny Buonocore
- 3,731
- 3
- 24
- 46
-
I'm glad you didn't go with your original idea because... If you traversed through the HTML you wouldn't find any controllers if people were using **ui-router** and `controllerAs`. – byxor Aug 23 '16 at 14:13
-
@BrandonIbbotson It's more of an in-house hacky solution, and all the controllers use `ng-controller` so it wouldn't be too much of a problem. I'm aware of `ui-router` but it's not being used for this. Any suggestions? – Danny Buonocore Aug 23 '16 at 14:17
-
`ngRoute` is also allowing to define controller for a specific html into js config files. – nubinub Aug 23 '16 at 14:21
-
@nubinub I am aware... which is why I asked this question. Like I said, this is an in-house solution where all the controllers are applied using `ng-controller`. For my purposes, a quick `querySelector` would do the job, I was wondering if there is a "more robust way", meaning taking into account this hurdles you mentioned. – Danny Buonocore Aug 23 '16 at 14:25
-
I can't think of any ways how. – byxor Aug 23 '16 at 14:33
-
_"where all the controllers are applied using ng-controller"_ So there's not a single directive that has its own controller? Very unlikely. Or you have a very specific definition of _"all the Angular controllers currently in use"_, which you did not share with us. By HTML you mean DOM, I hope. – a better oliver Aug 23 '16 at 15:04
-
@zeroflagL okay good catch, I should have been more specific... all the controllers that I, myself, have defined. – Danny Buonocore Aug 23 '16 at 15:13
-
@DannyBuonocore I've added an answer with an example – Michele Ricciardi Aug 25 '16 at 21:18
-
if you are using gulp, you can look at `gulp-angular-filesort`, this will not remove, but create sequence in index.html, by looking at that sequence, whatever files are left out or at the end of the sequence, means they are unused, though this assumes that, `you do actively remove unused controllers from dependency array` – harishr Sep 01 '16 at 05:16
3 Answers
6
Try to use _invokeQueue
It might be what you're looking for:
angular.module('MyApp')['_invokeQueue'].forEach(function(value){ console.log(value[2][0]) })
The _invokeQueue is populated with each service that is added to the module using the familiar angular.module('myModule').controller, angular.module('myModule').directive et al. calls. Each item in the queue is an array with three elements. The first is the provider that will invoke the service, the second is the method on the provider to use and the third element is an array of any arguments passed to the service.

Moamen Naanou
- 1,683
- 1
- 21
- 45
4
In any of your existing controller, copy and paste the below code will list you all of your controllers.
Note: Please change your application variable with 'app'.
var app = angular.module('myApp',[]);
angular.element(document).ready(function(){
var values = app._invokeQueue;
angular.forEach(values, function(value, key) {
if(value[0] == '$controllerProvider'){
console.log(value[2][0]);
}
});
});

ajin
- 1,136
- 1
- 11
- 22
3
You can do so by using the _invokeQueue
property of the module as shown in this example
var appModule = angular.module('plunker',[]);
angular.module('plunker').controller('TestCtrl', function(){});
angular.module('plunker').controller('TestCtrl2', function(){});
angular.module('plunker').controller('MainCtrl', function($scope) {
$scope.controllers = appModule._invokeQueue.filter(function(el){
return el[0] === "$controllerProvider";
}).map(function(el){
return el[2]["0"];
});
});

Michele Ricciardi
- 2,107
- 14
- 17