AngularJs 指令命名约定 [英] AngularJs directive naming conventions

查看:24
本文介绍了AngularJs 指令命名约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当定义angularJs指令时,我们必须以'camelCase'语法的形式命名它,但是当我们使用它时,我们必须以'camel-case'的形式命名它.问题是为什么需要这样做?

When angularJs directive is defined, we have to name it in form of 'camelCase' syntax but when we use it, we have to name it in form of 'camel-case'. Question is why this is required ?

我知道这是为了避免命名冲突(现在/将来),但为什么我们在定义和使用时必须以不同的方式命名.我们不能直接以 'camel-case' 的形式定义它吗?

I know that it is for avoiding naming conflicts(now/in future) but why we have to name it differently while defining and while using. Can't we define it directly in form of 'camel-case' ?

推荐答案

它很重要的原因有两个.

There are two reasons why it's important.

首先,HTML 属性不区分大小写,也就是说someName"和somename"是同一个属性.所以最好的风格是使用kebab-case"符号来分隔属性名称中的单词.这就是我们对 HTML 属性和标签名称使用属性名称"语法的原因.

First of all, HTML attributes are not case sensitive, meaning that "someName" and "somename" is the same attribute. So the best style is to use "kebab-case" notation to separate words in attribute name. That's why we use "attribute-name" syntax for HTML attributes and tag names.

另一方面,kebab-case 名称在 Javascript 中不是有效标识符,因此为了将此类名称用作 Angular 指令,我们必须使用详细的 括号符号.但由于在 Javascript 世界中,驼峰命名法是命名变量和对象属性的事实上的标准,Angular 使用 规范化(参见源代码) 将烤肉串名称转换为驼峰命名,反之亦然.

On the other hand, kebab-case names are not valid identifiers in Javascript so in order to use such names as Angular directives we would have to use verbose bracket notation. But since in Javascript world camelCase is standard de-facto for naming variables and object properties, Angular uses normalization (see source) to convert kebab-case names to camelCase, and vice versa.

这篇关于AngularJs 指令命名约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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