2016-04-12 23 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>