自定义AngularJS指令名称中包含破折号不起作用

编程入门 行业动态 更新时间:2024-10-19 08:46:09
本文介绍了自定义AngularJS指令名称中包含破折号不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我编写了以下Angular指令,该指令将向所有子代添加必需"属性:

I've written the following Angular directive that will add the "required" attribute to all children:

.directive("requireall", function($compile) { return { restrict: 'A', //only want it triggered for attributes compile: function(element, scope) { // Prevent infinite loop on compile element.removeAttr("requireall"); var allChildren = element.find('*'); allChildren.attr('required', 'required'); $compile(element)(scope); } } });

我真的很想称它为"require-all",但如果我重命名,它将无法正常工作.为什么"requireall"有效,而不是"require-all"?

I really want to call it "require-all" but if I rename it then it doesn't work anymore. Why is "requireall" working but not "require-all"?

推荐答案

Angular将camelCasing转换为snake-casing,因此您的requireall指令需要重命名为requireAll,然后可以在标记中使用require-all(或data-require-all(如果要正确标记自定义标签).一开始让我困惑了.

Angular converts camelCasing to snake-casing, so your requireall directive needs to be renamed to requireAll, then you can use require-all in your markup (or data-require-all if you want to correctly markup custom tags). Confused me for a while at first.

更多推荐

自定义AngularJS指令名称中包含破折号不起作用

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

发布评论

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

>www.elefans.com

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