How to get the value of the ngFor index



  • You could help me in a doubt, I have a form with an array where there is FormControl "id." Is there any way to receive the value of index to fill this field?

    <tr *ngFor="let item of invForm.controls.tool.controls; let i= index" [formGroupName]="i" >
        <td>
            <mat-form-field floatLabel='never'>
                <input matInput type="number" style="text-align: right" formControlName="Id"  value="{{i+1}}"  >
            </mat-form-field>
        </td>
    </tr>
    

    I did this. https://stackblitz.com/edit/invoice-id .

    [Resolved]

    I solved the problem with trackBy: trackByFn

    <tr *ngFor="let item of invForm.controls.tool.controls; let i= index ;trackBy: trackByFn" [formGroupName]="i"  >
    

    and component,

     trackByFn(index, item) {
    
     item.value.Id = index+1
    return index; 
    

    }

    I also leave the resolution here https://stackblitz.com/edit/invoice-id-resolvido



  • <ul>
      <li *ngFor="let item of especialidades; let i = index">
        {{i}} {{item.codigo}}
      </li>
    </ul>
    

    Syntax Angular ngFor:

    https://coryrylan.com/blog/angular-ng-for-syntax


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2