列出AngularJS中注册的自定义指令 [英] List registered custom directives in AngularJS

查看:86
本文介绍了列出AngularJS中注册的自定义指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写带有许多自定义指令的Web应用程序.有没有办法查看每个模块已注册的所有指令?

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?

推荐答案

模块具有_invokeQueue,其中包含模块的内容.像这样的功能:

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.

由于不确定您要使用哪种情况,因此我对此做了更通用的说明.

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

由于模块可以包括其他模块,因此您可以递归地收集子模块中的指令. http://jsfiddle.net/V7BUw/2/.

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/.

主要区别是您还需要对require数组中的每个模块重复:

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);
}

希望这会有所帮助!

这篇关于列出AngularJS中注册的自定义指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆