2016-04-12 22 views
4

IM在数据表中的工作角2角2 - * ngFor使用复选框和相应的标签

<table-component>是一个普通的<table>和里面我想乘采用了棱角分明的* ngFor <tr> S,像这样的:

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

<tr>有几个字段和一个checkboxlabel

我设置了checkbox的id属性,像这样:

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

凡{{我}}是从ngFor局部变量。它工作正常,但只适用于复选框。

当我尝试对label中的atrribute“for”执行相同操作时,我只会收到错误消息。

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> 

我的问题是:

如何设置了“”使用角2的ngFor标签的属性,所以他们指向正确的复选框?

回答

9

您应使用以下方法来设置attribut值:

<label [attr.for]="'checkbox'+i">{{person.finished}}</label>