Sub topic 5
Binding a FormArray in the template
A FormArray you build in the class does nothing on screen until the template connects each control to an input. This chapter covers how Angular resolves a path through a form, how to bind an array of controls, how to bind an array of groups, and which errors appear when a binding is wrong. It assumes you can create a FormGroup with FormBuilder, you know what Validators do, and you can import ReactiveFormsModule into a standalone component.
How Angular finds a control
Reactive form directives work by path. Each directive looks at its parent directive, adds its own name, and asks the root FormGroup for the control at that path. Three directives matter for arrays:
| Directive | Binds to | Name it expects |
|---|---|---|
| `formArrayName` | A `FormArray` that is a child of the parent group | A key in the parent group, such as `"items"` |
| `formGroupName` | A `FormGroup` inside the current group or array | A key, or a numeric index inside an array |
| `formControlName` | A `FormControl` inside the current group or array | A key, or a numeric index inside an array |
The key point is that an array has no field names. Its children are addressed by position. For an array of groups, [formGroupName]="i" means "the group at index i". For an array of plain controls, [formControlName]="i" means "the control at index i".
The path for the first input is items -> 0 -> name. If any directive in that chain is missing or misnamed, Angular cannot resolve the path and throws.
Binding an array of controls
When each entry is a single value, such as an email address, bind each input to its index with [formControlName]="i".
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>
<button type="button" (click)="addEmail()">Add email</button>
</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;
}
addEmail() {
this.emails.push(this.fb.control('', Validators.email));
}
}Three details to notice:
[formControlName]="i"uses property binding, soiis a number. WritingformControlName="i"would look for a control named the letteri.- The
formArrayNameelement must sit inside the element with[formGroup]. The directive finds its parent through Angular's dependency injection, so the nesting in the template must match the nesting in the form model. - The loop iterates
emails.controls, notemails.value. The value is an array of strings and has no control objects to bind to.
Binding an array of groups
Most real lists hold several fields per row. Put a [formGroupName]="i" element around each row. Inside it, the inputs use ordinary field names.
import { Component, inject } from '@angular/core';
import { NonNullableFormBuilder, ReactiveFormsModule, Validators } from '@angular/forms';
@Component({
selector: 'app-order-lines',
standalone: true,
imports: [ReactiveFormsModule],
template: `
<form [formGroup]="form">
<div formArrayName="items">
@for (item of items.controls; track item; let i = $index) {
<div [formGroupName]="i">
<label>
Name
<input formControlName="name" />
</label>
@if (item.controls.name.touched && item.controls.name.hasError('required')) {
<span class="error">Name is required (row {{ i + 1 }})</span>
}
<label>
Quantity
<input type="number" formControlName="quantity" />
</label>
</div>
}
</div>
</form>
`,
})
export class OrderLinesComponent {
private fb = inject(NonNullableFormBuilder);
form = this.fb.group({
items: this.fb.array([this.createItem(), this.createItem('Pen', 3)]),
});
get items() {
return this.form.controls.items;
}
createItem(name = '', quantity = 1) {
return this.fb.group({
name: [name, Validators.required],
quantity: [quantity, [Validators.required, Validators.min(1)]],
});
}
}The loop variable item is the actual FormGroup for that row, so you can read its state directly. In the template above, item.controls.name is typed as FormControl<string>. You can use item.valid, item.touched and item.controls.quantity.errors the same way, which avoids long lookups such as form.get('items.' + i + '.name').
Why a getter
The items getter is a shortcut for this.form.controls.items. It gives the template a short name and keeps the type information. Without it you would write form.controls.items.controls in the template, which works but is hard to read.
Binding a row without formGroupName
Because item is already a FormGroup, you can bind the row to it directly:
template: `
<form [formGroup]="form">
@for (item of items.controls; track item) {
<div [formGroup]="item">
<input formControlName="name" />
<input type="number" formControlName="quantity" />
</div>
}
</form>
`This skips formArrayName and the index. Each row is then its own root for path lookup. It is shorter, but it changes how the directives resolve paths. The inputs now look up name from the row group, and the array is no longer in the path. Use it for simple rows. Prefer formArrayName and [formGroupName] when you want the structure in the template to mirror the model, which helps when you later nest arrays inside rows.
Choosing a track expression
The @for block requires a track expression. For forms, you have two reasonable choices:
track itemuses the control object as the identity. When you remove row 0, Angular removes the DOM for that control and keeps the DOM for the others. Focus, cursor position and any local state stay with the right row.track $indexuses the position. When you remove row 0, Angular keeps the DOM nodes and rebinds them to the shifted controls. The rendering still shows correct values, but focus and browser-level state, such as an autofill highlight, can end up on the wrong row.
Tracking the control object is safe because a control keeps its identity for its whole life, and the list changes only when you push, insert or remove.
Common binding errors
| Message or symptom | Cause | Fix |
|---|---|---|
| `formArrayName must be used with a parent formGroup directive` | The `formArrayName` element is outside the `[formGroup]` element, or `ReactiveFormsModule` is not imported | Move it inside the form and check `imports` |
| `Cannot find control with path: 'items -> 0 -> name'` | The row has no `[formGroupName]`, or the field name differs from the key in the group | Match the names in the template and in `createItem` |
| `Cannot find control with name: 'i'` | You wrote `formControlName="i"` instead of `[formControlName]="i"` | Use property binding |
| Typing in one row changes another row | Two rows share the same `FormControl` instance, for example one control reused in two `push` calls | Create a new control for every entry |
| Input stays blank after loading data | The array has fewer controls than the data, so there is nothing to bind to | Push one control per record, as the data-loading chapter shows |
Checking your understanding
Exercise
A template has <div formArrayName="tags"> containing @for (t of tags.controls; track t; let i = $index) { <input formControlName="i" /> }, where tags is a FormArray of FormControls. The page throws a "Cannot find control" error. What is wrong, and what is the fix?
Show answer
formControlName="i" is a plain string attribute, so Angular looks for a control named "i" in the array and cannot find it. Arrays are addressed by number. Use property binding: <input [formControlName]="i" />.
Exercise
You loop over items.controls with track $index. A user removes the first row, and the focus ring jumps to what was the second row. Why, and which change fixes it?
Show answer
With track $index, Angular reuses the DOM node at each position and rebinds it to the shifted control, so browser-level state such as focus stays with the position rather than the control. Change the expression to track item, so each DOM node follows its control object and the removed control's node is the one destroyed.