6
Could help me in a doubt, I have a form with an array where there is the FormControl "id". Is there any way to receive the value of index to fill in 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 made this example in stackblitz.
[Solved]
I solved the problem with the trackBy: trackByFn
<tr *ngFor="let item of invForm.controls.tool.controls; let i= index ;trackBy: trackByFn" [formGroupName]="i" >
and in the component,
trackByFn(index, item) {
item.value.Id = index+1
return index;
}
I also leave the resolution on stackblitz
Thank you for your reply, but I have tried these methods and they have not worked
– Felipe Hideo
Try the yes I tested it right here. Post your html
– LR10
Create the index and show it in the right item, but I want to pass this index to the
FormControl. I’ll try to use thetrackBy: trackByFn.– Felipe Hideo
Put your Formcontrol ? where you want to pass!
– LR10
This will not work as it wishes to apply the value of
ito the form fieldID. What you are doing is displaying on the screen, which is not what the author wants to do.– celsomtrindade
That’s what Celso said, I needed to pass the index value
ifor the ID form. The example is here https://stackblitz.com/edit/invoice-id– Felipe Hideo
vc wants to take the index of the array of elements in your . ts ?
– LR10
Thanks for the @LR10 layout, I decided with the
trackBy: trackByFneven– Felipe Hideo