Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Angular not need a dash in component name

I wondered why Polymer elements need a dash in custom elements name like <my-component> while this is not necessary for Angular components especially as Angular components also use ShadowDOM.

** Edit** It doesn't even seem to be suggested good practice in Angular.

like image 400
Günter Zöchbauer Avatar asked Jan 17 '14 09:01

Günter Zöchbauer


People also ask

What is @component called in Angular?

The @Component decorator identifies the class immediately below it as a component class, and specifies its metadata. In the example code below, you can see that HeroListComponent is just a class, with no special Angular notation or syntax at all.

Why @component is used in Angular?

The Angular component allows us to provide a way for styling the component. This means that we can provide different CSS styling, rules, and other device-specific style configuration for a specific component. For that, the Angular component has metadata properties based on your different needs and requirements.

Can two components have same name in Angular?

Two or more components use the same element selector. Because there can only be a single component associated with an element, selectors must be unique strings to prevent ambiguity for Angular.

What is an Angular tag?

AngularJS is a JavaScript framework. It can be added to an HTML page with a <script> tag. AngularJS extends HTML attributes with Directives, and binds data to HTML with Expressions.


2 Answers

The HTML spec allows you to use unknown tags (<tab>, <panel>) without the HTML parser throwing a fit. To their benefit, Angular uses this behavior for their directives to make the components like native.

Tags that don't have a - inherit from HTMLUnknownElement. There's a good explanation of the upgrade process here: HTML5Rocks - Custom Elements - How elements are upgraded

Since Angular directives were designed in a time before the Custom Elements spec existed, they don't use a -. It's the standard that requires element names contain a -.

like image 105
ebidel Avatar answered Sep 23 '22 21:09

ebidel


The dash is not required by Angular since there is no technical reason to require it. However, all large projects I have worked on prefix all components and directives with a two character and then dash prefix, e.g "ab-tab".

First, using dashes in names makes your syntax compatible with the Custom Elements standard, although Angular doesn't depending the spec.

Second, it helps with organization. Standard Angular directives are all prefixed with 'ng-'. By using your own prefix, people reading your code will be able to quickly distinguish between components from different libraries.

like image 28
James deBoer Avatar answered Sep 22 '22 21:09

James deBoer