Sub topic 5
Tracking control state: touched, dirty, valid
This chapter assumes you can already bind an input with [(ngModel)], import FormsModule into a standalone component, and use template reference variables. Here you will learn the state flags Angular keeps for every control and how to read them in a template.
What Angular tracks
When ngModel is on an input, Angular creates a control behind it. The control carries boolean flags, and #name="ngModel" gives your template access to them.
| Flag | True when | Opposite |
|---|---|---|
| `touched` | The user focused the control and then left it (blur) | `untouched` |
| `dirty` | The user changed the value in the UI | `pristine` |
| `valid` | All validators pass | `invalid` |
| `pending` | An async validator is still running | none |
| `disabled` | The control is disabled and skipped by validation | `enabled` |
touched and dirty are independent. A user can tab through a field without typing, which makes it touched but pristine. A user can type and stay in the field, which makes it dirty but untouched.
Reading state in the template
Export the control into a reference variable with #name="ngModel". Then read the flags directly.
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
@Component({
selector: 'app-state-demo',
imports: [FormsModule],
template: `
<form #f="ngForm">
<input
name="email"
type="email"
[(ngModel)]="model.email"
#email="ngModel"
required
email
/>
<ul>
<li>touched: {{ email.touched }}</li>
<li>dirty: {{ email.dirty }}</li>
<li>valid: {{ email.valid }}</li>
<li>errors: {{ email.errors | json }}</li>
</ul>
<p>Whole form valid: {{ f.valid }}, dirty: {{ f.dirty }}</p>
</form>
`,
})
export class StateDemoComponent {
model = { email: '' };
}(The json pipe is built in and needs no import in current Angular versions.)
Open the page and watch the list. Initially the control is untouched, pristine, and invalid, because required fails on an empty value. Typing makes it dirty. Clicking elsewhere makes it touched.
The form (f) is an aggregate. f.dirty is true if any control is dirty. f.valid is true only if every enabled control is valid.
CSS classes Angular adds
Angular also mirrors these flags as classes on the input, and on the <form> element for the form-level state.
ng-untouched/ng-touchedng-pristine/ng-dirtyng-valid/ng-invalidng-pending
You can style them without writing any template logic:
input.ng-invalid.ng-touched {
border: 2px solid #b00020;
}
input.ng-valid.ng-dirty {
border: 2px solid #1b7f3b;
}Combining ng-invalid with ng-touched avoids showing red borders on a field the user has not visited yet.
Common errors
- Missing
nameattribute. Inside a<form>,ngModelregisters the control under itsname. Without one, Angular throws an error at runtime. - Expecting
dirtyafter code changes. Settingmodel.emailfrom TypeScript does not mark the control dirty. Only user input does. - Reading state from the wrong variable.
f.toucheddescribes the whole form. Use the control's own variable, such asemail.touched, for per-field messages. - Treating
invalidas "has an error to show". A pristine, untouched required field is invalid, but showing an error immediately is poor design. Checktouchedordirtyas well.
Exercise
A required text input has never been focused, and your component sets its model value to "abc" in ngOnInit. What are its touched, dirty, and valid values?
Show answer
touched is false and dirty is false, because only user interaction changes them. valid is true, because the value "abc" satisfies required.
Exercise
Which CSS selector styles only fields that are invalid and have already been visited by the user?
Show answer
input.ng-invalid.ng-touched