liveBook · Manning

Dirty property

As explained in Pro Angular 16

ng-dirty is an Angular CSS class applied to a form control after its value has been changed by the user.

Angular marks a form control as dirty after the user changes its contents. The control remains dirty even if the user restores its original value. section 13.4.2

The related ng-dirty CSS class enables feedback only after editing, avoiding error or success styling on untouched fields. Angular updates validation state as the user types or changes focus. section 6.5.2 section 13.4.2

Style edited fields by validation state

Use paired selectors such as input.ng-dirty.ng-invalid for a red border and input.ng-dirty.ng-valid for a green border. This leaves untouched controls visually unchanged while showing feedback after editing. section 6.5.2 section 13.4.2

Listing 6.25. The contents of the checkout.component.css file in the src/app/store folder
input.ng-dirty.ng-invalid { border: 2px solid #ff0000 }
input.ng-dirty.ng-valid { border: 2px solid #6bc502 }

View the complete stylesheet demonstrating ng-dirty combined with ng-valid and ng-invalid. section 6.5.2

How Pro Angular 16 covers it

How deep it goes
Defines the changed-content state, its persistence after reverting, and its CSS interaction, but defers broader validation classes.
Examples
Demonstrates checkout inputs and product-name validation using CSS borders and conditional validation messages.
What it assumes
Assumes basic Angular forms, validation rules, CSS selectors, and template bindings; distinguishes dirty state from untouched state.
What you can do after
Apply ng-dirty in CSS or templates to show validation feedback after editing and conditionally display messages for invalid controls.
How it connects
Links dirty state with pristine state, touched state, valid state, invalid state, validation attributes, and data binding.

This explainer was generated by Manning's knowledge graph from the text of Pro Angular 16. The section links point to the parts of the book it draws on.