自定义AngularJS指令名称中包含破折号不起作用 [英] Custom AngularJS directive including a dash in the name doesn't work

查看:79
本文介绍了自定义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指令名称中包含破折号不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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