Sub topic 2
FormControl: The Smallest Unit of Form State
This chapter assumes you know TypeScript generics, standalone Angular components, and how to import ReactiveFormsModule (covered in the previous chapter).
A FormControl tracks the value and state of a single input. Every larger structure in reactive forms, including FormGroup and FormArray, is built from controls like this one. To use reactive forms well, you need to understand what one control holds.
What a FormControl Holds
A control is an object in your component class. It holds more than a value:
| Property | Meaning |
|---|---|
| `value` | The current value |
| `status` | `'VALID'`, `'INVALID'`, `'PENDING'`, or `'DISABLED'` |
| `errors` | A map of validation errors, or `null` |
| `touched` / `dirty` | Whether the user has blurred the field or changed its value |
| `valueChanges` | An observable that emits each new value |
The model lives in code, so you can read and change all of this state without touching the DOM.
Creating a Control
The constructor takes an initial value and, optionally, an options object:
import { FormControl, Validators } from '@angular/forms';
const nickname = new FormControl('guest', {
nonNullable: true,
validators: [Validators.required],
});
console.log(nickname.value); // 'guest'
console.log(nickname.status); // 'VALID'
nickname.setValue('');
console.log(nickname.status); // 'INVALID'
console.log(nickname.errors); // { required: true }
nickname.reset();
console.log(nickname.value); // 'guest'Angular infers the type FormControl<string> from the initial value. Without nonNullable: true, the type is FormControl<string | null>, because reset() would then set the value to null. With the option set, reset() goes back to the initial value. Later chapters cover typing and validators in depth.
Binding a Single Control
A control can stand alone, with no group around it. The [formControl] directive connects it to an input:
import { Component } from '@angular/core';
import { FormControl, ReactiveFormsModule } from '@angular/forms';
@Component({
selector: 'app-nickname',
standalone: true,
imports: [ReactiveFormsModule],
template: `
<input [formControl]="nickname" />
<p>Hello, {{ nickname.value }}</p>
<button type="button" (click)="nickname.setValue('Ada')">Use "Ada"</button>
`,
})
export class NicknameComponent {
nickname = new FormControl('guest', { nonNullable: true });
}Data moves both ways. Typing updates the control, and calling setValue updates the input.
Common Mistakes
- Treating
valueas the source of truth for disabled controls. A disabled control keeps its value, but parent groups leave it out of theirvalue. The chapter on disabled controls explains this. - Forgetting
ReactiveFormsModule. If it is not imported, Angular reports that it can't bind toformControlbecause that isn't a known property ofinput. - Recreating the control. If you build a new control inside a getter or method that the template calls, every change detection pass creates a fresh object and the input loses its state. Create the control once, as a class field.
Exercise
You create const age = new FormControl(30); and later call age.reset(). What is the value, and what is the control's inferred type?
Show answer
The value is null, and the type is FormControl<number | null>. Without nonNullable: true, reset() clears the control to null, so the type includes null. Pass { nonNullable: true } to make reset() restore 30 and narrow the type to FormControl<number>.
Exercise
A control has Validators.required and holds the empty string. What do status and errors return?
Show answer
status is 'INVALID' and errors is { required: true }.