名称中包含破折号的自定义 AngularJS 指令不起作用 [英] Custom AngularJS directive including a dash in the name doesn't work
问题描述
我编写了以下 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 指令不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!