AngularJS,ng-click vs ng:click vs ngClick [英] AngularJS, ng-click vs ng:click vs ngClick
问题描述
我正在学习AngularJS,我对遇到的指令的不同用法感到有些困惑.
I'm learning AngularJS, and I'm a little confused by the different usages of directives that I'm coming across.
例如,有时我会看到类似(单击
For example, sometimes I see something like (ng colon click):
<tr ng:click="..." ...>
有时我看到(点击
<tr ng-click="..." ...>
,并且在Angular文档中,伪指令显示为" ngClick "(不带破折号或冒号的驼峰式).此外,在某些地方,我还看到了: data-ng-click
and in the Angular docs, directives are shown as "ngClick" (camelcase with no dash or colon). Additionally, in some places, I've seen: data-ng-click
这些不同形式之间有什么区别?
What's the difference between these different forms?
推荐答案
没什么区别,这完全取决于您的编程风格.我认为ng-click
是迄今为止最受欢迎的样式.
No difference, it all depends on your style of programming. ng-click
, I think, is by far the most popular style.
在创建自己的指令时,应始终在javascript中对其进行驼峰式显示,然后将其放在html中的某个元素上时,应使用小写版本,并用自己喜欢的样式分隔.我总是这样:
When you're creating your own directive, you should always camelcase it in the javascript, and then when you put it on an element in your html you should use the lowercase version separated by your favorite flavor. I always do it like so:
angular.module('Test', []).directive('testDirective', function(){
});
然后:
<div test-directive></div>
从角度文档:
最佳做法:最好使用破折号分隔的格式(例如ng-bind 对于ngBind).如果您想使用HTML验证工具,则可以 而是使用数据前缀版本(例如ngBind的data-ng-bind). 出于遗留原因,可以接受上面显示的其他形式,但我们 建议您避免使用它们.
Best Practice: Prefer using the dash-delimited format (e.g. ng-bind for ngBind). If you want to use an HTML validating tool, you can instead use the data-prefixed version (e.g. data-ng-bind for ngBind). The other forms shown above are accepted for legacy reasons but we advise you to avoid them.
这篇关于AngularJS,ng-click vs ng:click vs ngClick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!