Reactive Forms
In reactive forms you build the form model in TypeScript — FormControls grouped into FormGroups and FormArrays — and then connect the template to it. The model is explicit, strongly typed and easy to test, which is why most larger Angular apps use this approach.
The building blocks
| Class | Represents | Value type |
|---|---|---|
FormControl<T> |
One field | T |
FormGroup<{...}> |
A fixed set of named controls | An object |
FormArray<C> |
A variable-length list of controls | An array |
FormBuilder |
A helper service with shorthand factory methods | — |
A signup form
import { Component, inject } from '@angular/core';
import { FormBuilder, ReactiveFormsModule, Validators } from '@angular/forms';
@Component({
selector: 'app-signup',
imports: [ReactiveFormsModule],
template: `
<form [formGroup]="form" (ngSubmit)="submit()">
<label>Username <input formControlName="username" /></label>
<label>Email <input formControlName="email" type="email" /></label>
<div formGroupName="prefs">
<label><input type="checkbox" formControlName="newsletter" /> Newsletter</label>
<label>Language
<select formControlName="language">
<option value="en">English</option><option value="vi">Tiếng Việt</option>
</select>
</label>
</div>
<button [disabled]="form.invalid">Create account</button>
</form>
`,
styles: `label { display: block; margin-bottom: 8px; }`,
})
export class Signup {
private fb = inject(FormBuilder).nonNullable;
form = this.fb.group({
username: ['quang', [Validators.required, Validators.minLength(3)]],
email: ['quang@example.com', [Validators.required, Validators.email]],
prefs: this.fb.group({
newsletter: true,
language: 'vi',
}),
});
submit() {
const value = this.form.getRawValue(); // fully typed
console.log(value.username, value.prefs.language, value.prefs.newsletter);
}
}quang vi true
How the pieces connect:
[formGroup]="form"binds the<form>to the group.formControlName="username"binds an input to a control in the current group.formGroupName="prefs"steps into a nested group.inject(FormBuilder).nonNullablemakesreset()return controls to their initial values instead ofnull, and keeps the types non-nullable.
Reading and changing values
| API | Purpose |
|---|---|
form.value / form.getRawValue() |
Current value (raw includes disabled controls) |
control.setValue(v) |
Replace a value (a group needs every key) |
group.patchValue({...}) |
Update only some keys |
form.reset() |
Back to initial values, pristine and untouched |
control.valueChanges |
An Observable of every change |
control.disable() / enable() |
Exclude/include a control |
A form model in plain TypeScript
The core idea — controls that hold values and groups that combine them — is small enough to sketch:
class Control<T> {
constructor(public value: T, private readonly initial: T = value) {}
setValue(v: T) { this.value = v; }
reset() { this.value = this.initial; }
}
class Group<C extends Record<string, Control<unknown>>> {
constructor(public controls: C) {}
get value(): { [K in keyof C]: C[K]['value'] } {
const out = {} as { [K in keyof C]: C[K]['value'] };
for (const key in this.controls) out[key] = this.controls[key].value;
return out;
}
patchValue(partial: Partial<{ [K in keyof C]: C[K]['value'] }>) {
for (const key in partial) this.controls[key].setValue(partial[key]);
}
reset() { for (const key in this.controls) this.controls[key].reset(); }
}
const form = new Group({ username: new Control('quang'), age: new Control(30) });
form.patchValue({ age: 31 });
console.log(form.value);
form.reset();
console.log(form.value);{ username: 'quang', age: 31 }
{ username: 'quang', age: 30 }
Dynamic fields with FormArray
FormArray shines when the user can add and remove fields — phone numbers, invoice lines, skills:
import { Component } from '@angular/core';
import { FormArray, FormControl, ReactiveFormsModule } from '@angular/forms';
@Component({
selector: 'app-skills',
imports: [ReactiveFormsModule],
template: `
@for (ctrl of skills.controls; track ctrl; let i = $index) {
<div><input [formControl]="ctrl" /> <button (click)="skills.removeAt(i)">✕</button></div>
}
<button (click)="skills.push(newSkill())">+ Add skill</button>
<p>Value: {{ skills.value.join(', ') }}</p>
`,
})
export class Skills {
skills = new FormArray([new FormControl('TypeScript', { nonNullable: true }), new FormControl('RxJS', { nonNullable: true })]);
newSkill() {
return new FormControl('', { nonNullable: true });
}
}[formControl] binds a standalone control directly, without a surrounding formGroup.
Note: Recent Angular versions also include experimental signal forms, a signal-based forms API. Reactive forms remain the stable, widely used choice and everything here applies to them.
Test yourself
Which directive connects an input to a control inside the current FormGroup?
[(ngModel)]formControlNamengModelGroup[formArray]
What is the difference between setValue and patchValue on a group?
- None
setValuerequires every key;patchValueaccepts a subsetpatchValuevalidates,setValuedoesn'tsetValueis async
Which class models a list of fields whose length can change at runtime?
- FormGroup
- FormRecord
- FormArray
- FormBuilder