Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List registered custom directives in AngularJS

I am writing a web app with many custom directives. Is there a way to view all the directives that have been registered for each module?

like image 499
jelinson Avatar asked Jan 13 '23 06:01

jelinson


1 Answers

Modules have an _invokeQueue, which contains the contents of the module. A function like this:

function Directives(module) {
    var directives = [];
    var invokes = angular.module(module)._invokeQueue;
    for (var i in invokes) {
        if (invokes[i][1] === "directive") directives.push(invokes[i][2]);
    }
    return directives;
}

will run through the module and grab each element in the invoke queue that are tagged as directives.

Here is a fiddle where you can play with it.

EDIT: I made this slightly more generic, since I'm not sure which case you wanted.

Since modules can include other modules, this will allow you to recursively gather the directives that are in sub modules. http://jsfiddle.net/V7BUw/2/.

the main difference is you need to repeat for every module in the requires array as well:

for (var j in module.requires) {
    Directives(module.requires[j], directives);
}

Hope this helps!

like image 128
hassassin Avatar answered Jan 21 '23 20:01

hassassin