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.
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.
Reference
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]);
}
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With