I have angular reactive form consisting of few FormControl and one FormArray element.
competitionForm!: FormGroup;
this.competitionForm = new FormGroup({
name: new FormControl(this.competition.name, []),
about: new FormControl(this.competition.about, []),
rules: new FormControl(this.competition.rules, []),
competitorNames: new FormArray([]),
});
competitorNames contains array of FormControl with string as a value. I connected competitionForm with form on my html template. I am using custom input (app-input) component. It gets FormControl as input: [control].
<form [formGroup]="competitionForm">
<div formArrayName="competitorNames">
<ng-container *ngFor="let competitor of refCompetitorNames.controls">
<app-input
[control]="$any(competitor)"
[placeholder]="'name'"
[type]="'text'"
></app-input>
</ng-container>
</div>
</form>
get refCompetitorNames() {
return this.competitionForm.get('competitorNames') as FormArray;
}
Getting form data with this.competitionForm.value.competitorNames is always returning same data. It does not change when I enter new value in input element. Also, input elements are displayed with values inside. That means template form is binded with competitionForm.
This approach works when used on FormControl, but there is a problem with FormArray elements. Here is how template looks for single FormControl. Here I get updated data (if any input is changed) as I should.
Difference is in how I pass control.
<form [formGroup]="competitionForm">
<app-input
[control]="$any(competitionForm.controls).name"
[placeholder]="'name'"
[type]="'text'"
></app-input>
</form>
edit: Problem solved
I found where was the problem. Solution is to access form data in different way. With this.refCompetitorNames.at(i).value I get correct data.
I still don't know why this.competitionForm.value.competitorNames is not returning updated data (only in this case with FormArray).