Sub topic 10
Modeling Structured Data with Nested Groups
This chapter assumes you can already create a FormGroup, bind it with formGroup and formControlName, and use typed, non-nullable controls.
Real data is rarely flat. A customer has a name, but also a shipping address with its own street, city, and postal code. You could put every field in one flat group, but then the form's value no longer matches the shape your API expects. A nested group fixes that: a FormGroup can contain another FormGroup as one of its controls.
The Working Model
A FormGroup is itself an AbstractControl, so it can sit anywhere a FormControl can. The parent group then aggregates the child:
- Value: the parent's value includes the child's value as a nested object.
- Validity: if any control inside the child is invalid, the child is invalid, and so is the parent.
- State:
dirtyandtouchedbubble up from the controls to every ancestor group.
Building and Binding a Nested Group
In the template, formGroupName changes the scope. Inside the element that carries formGroupName, formControlName looks up names in the child group, not the parent.
import { Component } from '@angular/core';
import { FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
@Component({
selector: 'app-customer-form',
standalone: true,
imports: [ReactiveFormsModule],
template: `
<form [formGroup]="customer" (ngSubmit)="save()">
<input formControlName="name" placeholder="Name" />
<fieldset formGroupName="address">
<legend>Address</legend>
<input formControlName="street" placeholder="Street" />
<input formControlName="city" placeholder="City" />
<input formControlName="zip" placeholder="ZIP" />
</fieldset>
@if (customer.controls.address.invalid && customer.controls.address.touched) {
<p>Complete the address.</p>
}
<button type="submit" [disabled]="customer.invalid">Save</button>
</form>
`,
})
export class CustomerFormComponent {
customer = new FormGroup({
name: new FormControl('', { nonNullable: true, validators: [Validators.required] }),
address: new FormGroup({
street: new FormControl('', { nonNullable: true, validators: [Validators.required] }),
city: new FormControl('', { nonNullable: true, validators: [Validators.required] }),
zip: new FormControl('', { nonNullable: true }),
}),
});
save(): void {
// { name: '...', address: { street: '...', city: '...', zip: '...' } }
console.log(this.customer.getRawValue());
}
}Reaching Into Nested Controls
You have two ways to reach a nested control:
| Approach | Example | Type safety |
|---|---|---|
| `controls` chain | `customer.controls.address.controls.city` | Fully typed; typos fail at compile time |
| `get()` with a dotted path | `customer.get('address.city')` | Returns a possibly `null` control; typos fail only at runtime |
Prefer the controls chain with typed forms. Use get() when the path is built at runtime.
patchValue works on partial nested objects, so you can update one field deep in the tree:
this.customer.patchValue({ address: { city: 'Lisbon' } });Common Errors
Cannot find control with name: 'street': you usedformControlName="street"outside theformGroupName="address"element, so Angular looked forstreetin the parent group.- Using
[formGroup]for the child:[formGroup]on an inner element starts a new top-level binding. That works, but it loses the parent-relative scoping. UseformGroupNamefor children. setValuewith a partial nested object:setValuerequires every key at every level and throws if one is missing. UsepatchValuefor partial updates.
Exercise
A user fills in name and street but leaves city empty in the form above. What are the values of customer.controls.address.valid and customer.valid, and why?
Show answer
Both are false. The city control fails Validators.required, which makes its parent address group invalid, and an invalid child makes the customer group invalid too. Validity aggregates upward through every ancestor.
Exercise
Write one statement that changes only the ZIP code to '1100-001' without touching any other field.
Show answer
this.customer.patchValue({ address: { zip: '1100-001' } }); You could also write this.customer.controls.address.controls.zip.setValue('1100-001').