Sub topic 5
Binding a FormArray in the template
This chapter assumes you know TypeScript, Angular standalone components and templates, and the basics of FormControl, FormGroup and Validators. It also assumes you have already created a FormArray in code. Here you connect it to HTML.
Why binding a FormArray is different
A FormGroup has named children, so you bind each one with formControlName="email". A FormArray has no names. Its children are identified by position: 0, 1, 2. The template directives therefore use the index as the name.
Angular resolves each control by a path. A directive finds its parent through the enclosing directives, so the nesting in your HTML must match the nesting of your control tree.
The directives
| Directive | Binds to | Used for |
|---|---|---|
| `[formGroup]` | A `FormGroup` instance | The root of the form |
| `formArrayName` | A key in the parent group | The array container |
| `[formGroupName]="i"` | An index in the array | A group at position `i` |
| `[formControlName]="i"` | An index in the array | A plain control at position `i` |
Use formArrayName as a static string because it is a key in the parent group. Use property binding ([...]="i") for the index, because it is a number from your loop. Writing formGroupName="i" without brackets looks up a key named the literal string "i" and fails.
An array of plain controls
When each entry is one control, bind it directly by index.
import { Component, inject } from '@angular/core';
import { NonNullableFormBuilder, ReactiveFormsModule, Validators } from '@angular/forms';
@Component({
selector: 'app-email-list',
standalone: true,
imports: [ReactiveFormsModule],
template: `
<form [formGroup]="form">
<div formArrayName="emails">
@for (email of emails.controls; track email; let i = $index) {
<input type="email" [formControlName]="i" />
}
</div>
</form>
`,
})
export class EmailListComponent {
private fb = inject(NonNullableFormBuilder);
form = this.fb.group({
emails: this.fb.array([
this.fb.control('[email protected]', Validators.email),
this.fb.control('', Validators.email),
]),
});
get emails() {
return this.form.controls.emails;
}
}The emails getter exposes the typed array, so the template can loop over emails.controls. Looping over the controls, not over emails.value, keeps the loop in step with the real control objects.
An array of groups
When each row has several fields, add a [formGroupName] level inside the loop. This is the pattern used for order lines:
<div formArrayName="items">
@for (item of items.controls; track item; let i = $index) {
<div [formGroupName]="i">
<input formControlName="name" />
<input type="number" formControlName="quantity" />
</div>
}
</div>Inside [formGroupName]="i", the field names are ordinary strings again. The full path of the first name field is items.0.name, which you can pass to form.get('items.0.name').
Common errors
- "Cannot find control with path: 'items -> 0'": the
formArrayNamename does not match the key in your group, or an element is outside its parent directive. - "formArrayName must be used with a parent formGroup directive": the
formArrayNameelement is not inside the[formGroup]form. - "No directive found with exportAs 'ngForm'" or unknown property errors:
ReactiveFormsModuleis missing from the component'simports. - Rows that do not update: you looped over a plain array copy, not
controls.
Exercise
A form has FormGroup({ tags: FormArray<FormControl<string>> }). Write the template markup that renders one text input per tag, using tags.controls from a getter.
Show answer
<div formArrayName="tags">@for (tag of tags.controls; track tag; let i = $index) { <input [formControlName]="i" /> }</div>, placed inside an element with [formGroup]="form". The index must be bound with brackets because it is a number.