Angular 指令名称:只允许小写字母? [英] Angular directive name: only lower case letters allowed?

查看:27
本文介绍了Angular 指令名称:只允许小写字母?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码:

app.directive('abcabc', function (){ alert('directive');}); // working

但是

app.directive('abcAbc', function (){ alert('directive');}); // not working !
app.directive('abc-abc', function (){ alert('directive');}); // not working !

我做错了吗?还是 Angular 指令有特殊的命名规则?

Am I doing wrong? Or there are special naming rules for Angular directive?

推荐答案

AngularJS 试图让每个人都开心!

AngularJS attempts to make everyone happy!

有些人更喜欢使用数据属性,比如 data-abc-abc,我想是为了让验证者开心.其他人更喜欢使用像 abc:abc 这样的命名空间,而其他人更喜欢使用实际的指令名称 abcAbc.甚至全部大写ABC_ABC.或者像 x-abc-abc 这样的扩展属性.

Some people prefer to use data attributes, like data-abc-abc, I assume to keep validators happy. Other people prefer to use namespaces like abc:abc, and others prefer to use the actual directive name abcAbc. Or even all caps ABC_ABC. Or extension attributes like x-abc-abc.

AngularJS 规范了 HTML 中使用的名称,试图涵盖所有这些情况.data-x- 被剥离,剩下的用 :, -_ 作为单词边界.这使得 abcAbc 从上面提到的案例中得到,用于查找 JavaScript 中声明的指令.

AngularJS normalises the name used in HTML to attempt to cover all of these cases. data- and x- are stripped, the remainder camelcased with :, - and _ as word boundaries. This makes abcAbc from the cases mentioned above, which is used to look up the directive declared in JavaScript.

这都称为属性规范化(美国:属性规范化),可以在 AngularJS 文档中找到

This is all called attribute normalisation (US: attribute normalization) and can be found in the AngularJS documentation and source code.

这篇关于Angular 指令名称:只允许小写字母?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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