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

查看:24
本文介绍了列出在 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?

推荐答案

模块有一个 _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.

这是一个fiddle,您可以在其中玩它.

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

主要区别在于您还需要对 requires 数组中的每个模块重复:

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天全站免登陆