AngularJs directive naming conventions

前端 未结 2 1211
半阙折子戏
半阙折子戏 2020-12-05 19:23

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 thi

2条回答
  •  清歌不尽
    2020-12-05 19:54

    There are two reasons why it's important.

    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.

    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.

提交回复
热议问题