Angular 4 - 将变量设置为for循环的索引

时间:2017-11-28 15:54:12

标签: angular

我有一个像这样的简单循环:

<div *ngFor="let treatment of data.treatments; let order = index">
    {{treatment.order}}
    <input type="text" [(ngModel)]="treatment.name.dutch" >
</div>

现在我想在我的模板代码中设置treatment.order = order。所以列表总是

1 ...
2 ...
3 ...

但是变量treatment.order也将在列表中具有相应的顺序。我似乎找不到办法做到这一点

1 个答案:

答案 0 :(得分:1)

创建一个用于对数组进行排序的排序管道

More about pipes

stackoverflow question about sorting pipe

示例:

<div *ngFor="let treatment of data.treatments | sortOrder; let order = index">
    {{treatment.order}}
    <input type="text" [(ngModel)]="treatment.name.dutch" >
</div>