列出在 AngularJS 中注册的自定义指令

编程入门 行业动态 更新时间:2024-10-22 15:24:37
本文介绍了列出在 AngularJS 中注册的自定义指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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

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

Since modules can include other modules, this will allow you to recursively gather the directives that are in sub modules. http://jsfiddle/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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-21 22:02:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1011332.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   出在   指令   AngularJS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!