The example in Angular official docs show the following code:
@Component({
template: `
<form [formGroup]="form">
<div formArrayName="cities">
<div *ngFor="let city of cities.controls; index as i">
<input [formControlName]="i">
</div>
</div>
</form>
`,
})
export class NestedFormArray {
form = new FormGroup({
cities: new FormArray([
new FormControl('SF'),
new FormControl('NY'),
]),
});
get cities(): FormArray { return this.form.get('cities') as FormArray; }
}
I can't understand why we need the formArrayName directive when the code works even without it?
<form [formGroup]="form">
<div *ngFor="let city of cities.controls; index as i">
<input [formControlName]="i">
</div>
</form>