Sub topic 1
Why Reactive Forms: Comparing the Two Angular Form Models
Prerequisites
This chapter assumes you know TypeScript generics, Angular standalone components, template data binding, and basic RxJS observables.
Reactive vs Template-Driven
Angular has two form models. Template-driven forms build their state from ngModel directives in the HTML. Reactive forms build the state explicitly in component code, and the template only binds to it.
| Aspect | Template-driven | Reactive |
|---|---|---|
| Where the model lives | Template | Component class |
| Data flow | Asynchronous, via directives | Synchronous, direct access |
| Typing | Loose | Strictly typed |
| Testing | Needs rendering | Test the model alone |
| Best for | Small, simple forms | Complex, dynamic, validated forms |
Choose reactive forms when the form has validation rules, fields that change at runtime, or logic you want to unit test.
Importing ReactiveFormsModule
Standalone components add ReactiveFormsModule to their imports array. This module provides the formGroup, formControlName, and formControl directives.
FormControl and FormGroup
A FormControl tracks one field's value, validity, and interaction state. A FormGroup combines named controls into one object, and its value and validity are computed from its children.
Binding and Updating Values
import { Component } from '@angular/core';
import { FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms';
@Component({
selector: 'app-profile',
standalone: true,
imports: [ReactiveFormsModule],
template: `
<form [formGroup]="profileForm">
<input formControlName="name" />
<input formControlName="email" />
</form>
<button (click)="loadAll()">Load</button>
<button (click)="changeEmail()">Patch email</button>
<button (click)="profileForm.reset()">Reset</button>
<pre>{{ profileForm.value | json }}</pre>
`,
})
export class ProfileComponent {
profileForm = new FormGroup({
name: new FormControl('', { nonNullable: true }),
email: new FormControl('', { nonNullable: true }),
});
loadAll(): void {
this.profileForm.setValue({ name: 'Ada', email: '[email protected]' });
}
changeEmail(): void {
this.profileForm.patchValue({ email: '[email protected]' });
}
}Use [formGroup] on the <form> element and formControlName on each input. A control that belongs to no group binds with [formControl].
setValuereplaces the whole value. It throws an error if a key is missing or unknown.patchValueupdates only the keys you pass.resetreturns each control to its initial value when the control isnonNullable. Otherwise it sets the value tonull.
The corrected imports line is imports: [ReactiveFormsModule, JsonPipe], with import { JsonPipe } from '@angular/common';.
Exercise
You call profileForm.setValue({ email: '[email protected]' }). What happens, and what should you call instead?
Show answer
setValue throws an error because the name key is missing. Call patchValue({ email: '[email protected]' }) to update only the email.
Exercise
After reset(), what value does a control created with new FormControl('Ada') hold, and what about one created with { nonNullable: true }?
Show answer
The default control becomes null. The non-nullable control returns to its initial value, 'Ada'.