2

I have a simple loop like this:

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

Now I would like to set treatment.order = order in my template code. So that the list would always be

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

But also that the variable treatment.order would then have the corresponding order in the list. I can't seem to find a way to do this

Aleksey Solovey
  • 4,153
  • 3
  • 15
  • 34
user2657943
  • 2,598
  • 5
  • 27
  • 49

1 Answers1

1

create a sort pipe for sorting the array

More about pipes

stackoverflow question about sorting pipe

Example:

<div *ngFor="let treatment of data.treatments | sortOrder; let order = index">
    {{treatment.order}}
    <input type="text" [(ngModel)]="treatment.name.dutch" >
</div>
G.Vitelli
  • 1,229
  • 9
  • 18