简体   繁体   English

Angular指令名称:只允许小写字母?

[英]Angular directive name: only lower case letters allowed?

My code: 我的代码:

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

but

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

Am I doing wrong? 我做错了吗? Or there are special naming rules for Angular directive? 或者Angular指令有特殊的命名规则?

AngularJS attempts to make everyone happy! AngularJS试图让每个人都开心!

Some people prefer to use data attributes, like data-abc-abc , I assume to keep validators happy. 有些人喜欢使用数据属性,比如data-abc-abc ,我假设让验证者感到高兴。 Other people prefer to use namespaces like abc:abc , and others prefer to use the actual directive name abcAbc . 其他人更喜欢使用像abc:abc这样的命名空间,而其他人更喜欢使用实际的指令名称abcAbc Or even all caps ABC_ABC . 甚至全部大写ABC_ABC Or extension attributes like x-abc-abc . 或者像x-abc-abc这样的扩展属性。

AngularJS normalises the name used in HTML to attempt to cover all of these cases. AngularJS规范化HTML中使用的名称以尝试涵盖所有这些情况。 data- and x- are stripped, the remainder camelcased with : , - and _ as word boundaries. data-x-被剥离,其余部分带有:-_作为字边界。 This makes abcAbc from the cases mentioned above, which is used to look up the directive declared in JavaScript. 这使得abcAbc来自上面提到的情况,用于查找在JavaScript中声明的指令。

This is all called attribute normalisation (US: attribute normalization) and can be found in the AngularJS documentation and source code . 这称为属性规范化(US:属性规范化),可以在AngularJS文档源代码中找到

You should use dash-separated names inside the html and camelCase for the corresponding name in the directive. 您应该在html和camelCase中使用以短划线分隔的名称作为指令中的相应名称。

As you can read on the doc: Angular uses name-with-dashes for attribute names and camelCase for the corresponding directive name) 正如您可以在doc上阅读的那样: Angular使用name-with-dashes作为属性名称,使用camelCase作为相应的指令名称)

Here: http://docs.angularjs.org/tutorial/step_00 这里: http//docs.angularjs.org/tutorial/step_00

好吧,指令名称必须全部小写,至少在AngularJS版本1.4.9中,否则我得到一个$ inject无法找到错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM