Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all Angular controllers [duplicate]

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.

like image 556
Danny Buonocore Avatar asked Aug 23 '16 14:08

Danny Buonocore


2 Answers

Try to use _invokeQueue It might be what you're looking for:

angular.module('MyApp')['_invokeQueue'].forEach(function(val‌​ue){ 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

like image 68
Moamen Naanou Avatar answered Nov 11 '22 16:11

Moamen Naanou


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]);
            }
        });
    });
like image 4
ajin Avatar answered Nov 11 '22 16:11

ajin