Sub topic 3
FormControl: The Smallest Unit of Form State
This chapter assumes you know TypeScript generics, can write a standalone Angular component, and have imported ReactiveFormsModule as shown in the previous chapter. It covers what a single FormControl holds, how to create one, and the mistakes people make most often when they first use it.
What a FormControl Is
A FormControl is an object that tracks the state of one form field. It is the base unit of reactive forms. FormGroup and FormArray, covered in later chapters, are containers of controls and share the same base class, AbstractControl.
A control exists in your component class whether or not any input element is bound to it. The template only reflects it. This is why you can read, change, and test a control without rendering anything.
Each control keeps track of the following:
| Property | Meaning | Typical values |
|---|---|---|
| `value` | The current value | `'ada'`, `42`, `null` |
| `defaultValue` | The value used by `reset()` on non-nullable controls | the initial value |
| `status` | The validation status | `'VALID'`, `'INVALID'`, `'PENDING'`, `'DISABLED'` |
| `errors` | Errors from validators, or `null` | `{ required: true }` |
| `pristine` / `dirty` | Whether the user has changed the value through the UI | `true` / `false` |
| `untouched` / `touched` | Whether the user has blurred the field | `true` / `false` |
| `enabled` / `disabled` | Whether the control takes part in validation and its parent's value | `true` / `false` |
Creating a Control
The constructor takes an initial value and an optional second argument. That argument can be a validator, an array of validators, or an options object.
import { FormControl, Validators } from '@angular/forms';
// Type: FormControl<string | null>
const nickname = new FormControl('');
// Type: FormControl<string>
const email = new FormControl('', {
nonNullable: true,
validators: [Validators.required, Validators.email],
});
// Starts disabled; the initial value is "boxed" with its disabled state
const country = new FormControl({ value: 'NZ', disabled: true });
console.log(nickname.value); // ''
console.log(email.status); // 'INVALID' (required fails on '')
console.log(email.errors); // { required: true }
console.log(country.status); // 'DISABLED'Validators run as soon as the control is constructed. A required field with an empty initial value is invalid immediately, before the user has touched it. That is why error messages are normally gated on touched or dirty. A later chapter covers how to do that.
Reading and Changing the Value
Three methods change the value from code:
setValue(v)replaces the value. On aFormControl,patchValue(v)behaves the same way. The two only differ on groups and arrays.reset()restores the value and also resets the flags topristineanduntouched.reset(v)resets the flags and sets the value tov.
import { FormControl } from '@angular/forms';
const loose = new FormControl('guest');
const strict = new FormControl('guest', { nonNullable: true });
loose.setValue('ada');
strict.setValue('ada');
loose.reset();
strict.reset();
console.log(loose.value); // null (nullable controls reset to null)
console.log(strict.value); // 'guest' (non-nullable controls reset to defaultValue)The null result from reset() is the reason the type of new FormControl('guest') is FormControl<string | null>. If you set nonNullable: true, reset() returns the control to its initial value, so the type narrows to string. The chapter on typed forms explains this in detail. For a single control, the practical rule is to use nonNullable: true unless "no value" is a real state in your domain.
Options on setValue
setValue accepts a second argument that controls side effects:
import { FormControl } from '@angular/forms';
const qty = new FormControl(1, { nonNullable: true });
qty.valueChanges.subscribe((v) => console.log('changed to', v));
qty.setValue(2); // logs "changed to 2"
qty.setValue(3, { emitEvent: false }); // silent: no valueChanges or statusChanges
console.log(qty.value); // 3Use emitEvent: false when you update a control from inside its own change handler, or from a handler that would otherwise trigger it again. Without it you can create an infinite loop.
Interaction Flags Are User-Driven
Calling setValue from code does not mark a control dirty or touched. Those flags describe what the user did. Directives set them when the bound element fires input and blur events. You can set them yourself:
import { FormControl, Validators } from '@angular/forms';
const name = new FormControl('', { nonNullable: true, validators: Validators.required });
name.setValue('Ada');
console.log(name.dirty); // false
name.markAsDirty();
name.markAsTouched();
console.log(name.dirty, name.touched); // true true
name.reset();
console.log(name.pristine, name.untouched); // true truemarkAsTouched() is common in submit handlers. It makes error messages appear on fields the user skipped.
Binding a Standalone Control
A control that does not belong to a group binds with the [formControl] directive. The next chapters cover binding in full. Here is a minimal component that shows the model and the view staying in sync:
import { Component } from '@angular/core';
import { FormControl, ReactiveFormsModule } from '@angular/forms';
@Component({
selector: 'app-nickname',
standalone: true,
imports: [ReactiveFormsModule],
template: `
<input [formControl]="nickname" />
<p>Value: {{ nickname.value }} | dirty: {{ nickname.dirty }}</p>
<button type="button" (click)="nickname.setValue('Ada')">Set from code</button>
<button type="button" (click)="nickname.reset()">Reset</button>
`,
})
export class NicknameComponent {
nickname = new FormControl('guest', { nonNullable: true });
}Typing in the input sets dirty to true. Clicking "Set from code" updates the input but leaves dirty unchanged.
Common Errors
| Symptom | Cause | Fix |
|---|---|---|
| `Can't bind to 'formControl' since it isn't a known property of 'input'` | `ReactiveFormsModule` is missing from the component's `imports` | Add it to `imports` |
| The value is `null` after `reset()` and TypeScript complains downstream | The control is nullable | Add `nonNullable: true` |
| Using `[disabled]="true"` on the input logs a warning, and the control stays enabled | The DOM attribute fights the model | Use `{ value, disabled: true }` or `control.disable()` |
| The input stops updating after you reassign the property | The template is still bound to the old instance | Mutate the existing control with `setValue` instead of creating a new `FormControl` |
| An error message shows on page load | Validators ran on the initial value | Gate the message on `touched` or `dirty` |
Exercise
You create const age = new FormControl(18, { nonNullable: true, validators: Validators.min(21) }), call age.setValue(30), and then call age.reset(). What are age.value, age.status, and age.dirty after each step?
Show answer
After construction the value is 18, the status is 'INVALID' because 18 is below 21, and dirty is false. After setValue(30) the value is 30, the status is 'VALID', and dirty is still false because programmatic changes do not mark a control dirty. After reset() the value returns to the default of 18, the status is 'INVALID' again, and dirty is false.
Exercise
A handler subscribed to price.valueChanges rounds the number and writes it back with price.setValue(Math.round(v)). The browser tab freezes. Why, and what is the minimal fix?
Show answer
Each setValue emits valueChanges again, so the handler calls itself over and over. Pass { emitEvent: false } when writing the rounded value back: price.setValue(Math.round(v), { emitEvent: false }). Another fix is to call setValue only when the rounded value differs from the current value.
Exercise
Why does new FormControl('') have the type FormControl<string | null> instead of FormControl<string>?
Show answer
Calling reset() with no argument sets a nullable control's value to null. TypeScript includes null in the type to reflect that. Passing nonNullable: true makes reset() restore the initial value, so the type becomes FormControl<string>.