为什么我们在AngularJS中使用脊柱案例作为指令

时间:2014-07-01 03:13:10

标签: angularjs

When we talk about directives, we’ll use lowerCamelCase method (e.g. ngClick) 
to refer to the directive. This is because in the AngularJS source code it is 
defined as a function called ngClick. However, when we use a directive in HTML 
we use “kabob case” (e.g. ng-click). Basically: ngClick and ng-click refer 
to the same thing, AngularJS automatically converts between the two. It can be a 
little confusing at first, but the idea is that it makes each code look better in 
context.

这是http://www.ng-newsletter.com/posts/how-to-learn-angular.html中给出的内容 同时解释AngularJS中的指令。

ngClickng-click引用相同的内容我们可以使用此方法吗?

<button ngClick="runWhenButtonClicked()">Click me</button>

而不是这种方法

<button ng-click="runWhenButtonClicked()">Click me</button>

我的问题是,使用脊柱病是一个规则还是我们也可以使用camelCase?

谢谢。

2 个答案:

答案 0 :(得分:0)

camelCase指令在你的部分胜利甚至工作,所以<button ngClick="runWhenButtonClicked()">Click me</button>没有做任何事情。

Angular会进行翻译,所以你的指令的定义总是以驼峰为基础,而使用该指令总是以脊柱为基础。

试一试:http://jsfiddle.net/jorgthuijls/2nH4f/

在定义中:

myApp.directive('myDirective1', function () {
    return {
        replace: true,
        template: '<span>I was used spinal-cased</span>'
    };
});

myApp.directive('myDirective2', function () {
    return {
        replace: true,
        template: '<span>I was used camelCased</span>'
    };
});

在部分:

<div my-directive1>i'm not replaced.</div>
<div myDirective2>i'm not replaced.</div>

答案 1 :(得分:0)

  

Angular使用脊椎案例来定制其自定义属性和camelCase for   实现它们的相应指令

https://docs.angularjs.org/tutorial/step_00#what-is-the-code-doing-