angularjs-de/angular2-tutorial

order-component id

sja opened this issue · 3 comments

sja commented

Hi!

In order.component.html you have a label with for="suche" but the input has no id attr. How to deal with that in a component? If I add an id, I cannot re-use the component. Once I added the scope-id to the id (id="my_component{{scope.$id}}") but of course thats not very elegant. Are there any best practises for that?

do not use ids --> use another attributes as selector, e.g. data-my-component.

But keep in mind, if you really want to build a component the css-selector should be an element-name, not a class or attribute!

attributes, classes are used for structural/attribute directives! not components

sja commented

Hm, ok. As of HTML5 spec you either have to use the for-attribute to bind a label to an input or nest them.

The thing is, that if you bind them together, you can click on a label to set a focus on the corresponding input. I don't want to re-implement that with JS, of course.

yeah, for sure, but in this example you can remove the "for" so i forgot to set the "id".

And you have to decide --> is my component a part of a route definition or do i want a reusable component. To reuse them you have to take about unique ids. Maybe adding timestamp or a shorter random string is enough.

But you have to question if you can have multiple instances of a form on one route.