Sub topic 7
Repeating groups of fields
This chapter assumes you know TypeScript, Angular standalone components, and the basics of FormControl, FormGroup, and Validators. You should also know how to add and remove controls in a FormArray.
Why a FormArray of FormGroups
A list of single controls fits something like email addresses. Most real lists need several fields per row, such as an order line with a name and a quantity. For that, each entry in the array is a FormGroup. The array grows and shrinks, and each row keeps its own fields and validation state.
Building the model
Write one factory method that returns a row. The form uses it for the initial row, and addItem() uses it for every new row. This keeps all rows the same shape.
createItem(name = '', quantity = 1) {
return this.fb.group({
name: [name, Validators.required],
quantity: [quantity, [Validators.required, Validators.min(1)]],
});
}
form = this.fb.group({
items: this.fb.array([this.createItem()]),
});
get items() {
return this.form.controls.items;
}The items getter gives the template and the methods a typed handle on the array.
Binding the rows
Two directives connect the template to the model:
formArrayName="items"on a wrapper element selects the array inside the parent group.[formGroupName]="i"on each row selects the group at indexi. Array entries are addressed by number, so you bind the index and not a string.
Inside a row, formControlName works as it does in any group.
Complete example
import { Component, inject } from '@angular/core';
import { NonNullableFormBuilder, ReactiveFormsModule, Validators } from '@angular/forms';
@Component({
selector: 'app-order-form',
standalone: true,
imports: [ReactiveFormsModule],
template: `
<form [formGroup]="form" (ngSubmit)="submit()">
<div formArrayName="items">
@for (item of items.controls; track item; let i = $index) {
<div [formGroupName]="i">
<input formControlName="name" placeholder="Name" />
<input type="number" formControlName="quantity" />
<button type="button" (click)="removeItem(i)">Remove</button>
</div>
}
</div>
<button type="button" (click)="addItem()">Add item</button>
<button type="submit" [disabled]="form.invalid">Submit</button>
</form>
`,
})
export class OrderFormComponent {
private fb = inject(NonNullableFormBuilder);
form = this.fb.group({
items: this.fb.array([this.createItem()]),
});
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)]],
});
}
addItem() {
this.items.push(this.createItem());
}
removeItem(index: number) {
this.items.removeAt(index);
}
submit() {
console.log(this.form.getRawValue());
}
}The @for loop uses track item. Each row is a distinct control object, so Angular keeps the DOM for the rows that stay when you remove one.
Common errors
- Missing
formArrayName. Without the wrapper,formGroupNamethrows an error saying it must be used with a parentformGrouporformArrayNamedirective. - Binding a string to
formGroupName. Use the numeric index.formGroupName="0"as a literal string works, but it does not follow the loop. - Different shapes per row. If rows come from different code paths, the form value becomes inconsistent. Use the one
createItem()method. - Using
<form>buttons withouttype="button". A button inside a form defaults to submit, so "Add item" and "Remove" would submit the form.
Exercise
Exercise
You add a notes field to each order row. Which two places in the component and template must change so that new and initial rows both have it?
Show answer
Add notes: [''] to the group in createItem(), so every row has the control. Then add <input formControlName="notes" /> inside the [formGroupName]="i" element. No other change is needed, because the initial row and addItem() both call createItem().