Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 - *ngFor with checkbox and corresponding label [duplicate]

Tags:

angular

im working on a data-table in Angular 2.

My <table-component> is a regular <table> and inside I'm trying to multiply <tr>s using Angular's *ngFor, like this:

<tr *ngFor="#person of persons; #i = index">

Every <tr> has a couple of fields and one checkbox with a label.

I set the id attribute of the checkbox like so:

<input type="checkbox" id="checkbox{{i}}">
<label for="checkbox{{i}}">{{person.finished}}</label>

Where {{i}} is the local variable from ngFor. And it works fine, but only for the checkbox.

When I try to do the same for the atrribute "for" in the label I only get errors.

Unhandled Promise rejection: Template parse errors:
Can't bind to 'for' since it isn't a known native property ("
   <div>
        <input type="checkbox" id="checkbox{{i}}">
        <label [ERROR ->]for="checkbox{{i}}">{{person.finished}}</label>
    </div>
</td>

My question is:

How can I set the "for" attribute of the labels using Angular 2's ngFor, so they point to the right checkbox?

like image 450
Alexander Ciesielski Avatar asked Apr 12 '16 14:04

Alexander Ciesielski


1 Answers

You should use the following to set the attribut value:

<label [attr.for]="'checkbox'+i">{{person.finished}}</label>
like image 117
Thierry Templier Avatar answered Oct 09 '22 16:10

Thierry Templier