Sub topic 14
Reacting to valueChanges and statusChanges
This chapter assumes you know how to build a FormControl and FormGroup, and that you are comfortable with basic RxJS operators such as pipe, map, and debounceTime.
Every control (FormControl, FormGroup, FormArray) exposes two observables:
valueChangesemits the new value each time the control's value changes.statusChangesemits the new validation status:'VALID','INVALID','PENDING', or'DISABLED'.
These observables let you respond to user input in code, for example to run a search, save a draft, or enable another field.
How emissions flow
A change to a child control first updates the child, then bubbles up through each parent. Every level emits its own valueChanges and statusChanges.
A debounced search box
Subscriptions on these observables do not end on their own. Use takeUntilDestroyed so the subscription stops when the component is destroyed.
import { Component, DestroyRef, OnInit, inject } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { FormControl, ReactiveFormsModule } from '@angular/forms';
import { debounceTime, distinctUntilChanged } from 'rxjs/operators';
@Component({
selector: 'app-search-box',
standalone: true,
imports: [ReactiveFormsModule],
template: `<input [formControl]="query" placeholder="Search" />`,
})
export class SearchBoxComponent implements OnInit {
query = new FormControl('', { nonNullable: true });
private destroyRef = inject(DestroyRef);
ngOnInit(): void {
this.query.valueChanges
.pipe(debounceTime(300), distinctUntilChanged(), takeUntilDestroyed(this.destroyRef))
.subscribe((term) => console.log('search for', term));
}
}You have to pass this.destroyRef here because ngOnInit runs outside an injection context. If you subscribe in the constructor, you can call takeUntilDestroyed() with no argument.
Watching status
statusChanges works well for tracking a form-level flag:
this.profileForm.statusChanges
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe((status) => (this.canSave = status === 'VALID'));Common errors
| Mistake | Symptom | Fix |
|---|---|---|
| Expecting an initial emission | Logic never runs until the user types | Add `startWith(control.value)` |
| Calling `setValue` on the same control inside its own subscription | Infinite loop or stack overflow | Pass `{ emitEvent: false }` or update a different control |
| Not unsubscribing | Memory leaks, handlers firing after navigation | `takeUntilDestroyed` or the `async` pipe |
| Reading `group.value` and expecting disabled fields | Disabled fields are missing | Use `getRawValue()` |
Exercise
You subscribe to form.controls.country.valueChanges to clear the state control whenever the country changes. On page load you call form.patchValue(savedAddress), and the saved state gets wiped. Why, and how do you fix it?
Show answer
patchValue sets country, which emits valueChanges, so your handler runs and clears state. Call form.patchValue(savedAddress, { emitEvent: false }) when loading saved data, so only real user changes trigger the handler.
Exercise
Your valueChanges handler should also run once with the control's current value when the component starts. Which operator do you add?
Show answer
startWith(this.query.value), placed before debounceTime in the pipe, so the stream emits the current value right away.