ngFor如何使用两个变量?

时间:2019-05-27 16:51:52

标签: angular

这是我的HTML代码:

<ng-containte *ngFor="let item of array"; let j = item>

<ng-containte *ngFor="let item of array"; let i = index>

<input (ngModelChange) = "fun(value,2,item)" >

使用以下javascript:

fun(value:string, index:number, item:number){
 do something...
}

如何使用一个ngFor来控制“ i”和“ j” 这样它就不会循环2次?

1 个答案:

答案 0 :(得分:3)

只需使用嵌套的ng-container

<ng-container *ngFor="let item of array"; let j = index>
  <ng-container *ngFor="let item of array"; let i = index>
    <input (ngModelChange) = "fun(value,i,j)" >
  </ng-container>
</ng-container>

如果您真的想使用item并且帖子中没有错字,则无需重复:

<ng-container *ngFor="let item of array">
  <ng-container *ngFor="let item2 of array"; let i = index>
    <input (ngModelChange) = "fun(value,i,item)" >
  </ng-container>
</ng-container>