2017-02-13 77 views
6

我在Angular2中有兩個嵌套的* ngFor循環。我想維護一個計數器,每次循環中心的代碼重複時都會增加計數器。在Angular2中嵌套ngFor循環的計數器

計數器應該從0到的調整的次數時代葉片

<tr *ngFor="let adjustment of adjustments"> 

    <td *ngFor="let blade of blades"> 

     <span> Counter = ????????? </span> 

    </td> 

    </tr> 

回答

12

數計數器變量添加到這兩個* ngFor聲明,說i和j,然後做數學作爲表達。這裏是代碼:

<tr *ngFor="let adjustment of adjustments; let i = index"> 

    <td *ngFor="let blade of blades; let j = index"> 

     <span> Counter = {{ (i * blades.length) + j }} </span> 

    </td> 

    </tr>