Visual Cues for Users
:validpseudo-selectors for its input fields.
border: 2px solid green;
border: 2px solid red;
Unfortunately, this system is rather unsophisticated and would require more manual effort in order to work with complex forms or user behavior.
Rather than writing extra code, and creating and enforcing your own CSS classes, to manage these behaviors, Angular provides you with several classes, already accessible on your inputs.
/* field value is valid */
/* field value is invalid */
/* field has not been clicked in, tapped on, or tabbed over */
/* field has been previously entered */
/* field value is unchanged from the default value */
/* field value has been modified from the default */
Note the three pairs:
- valid / invalid
- untouched / touched
- pristine / dirty
These pairs can be used in many combinations in your CSS to change style based on the three separate flags they represent. Angular will toggle between the pairs on each input as the state of the input changes.
/* field has been unvisited and unchanged */
/* field has been previously visited, and is invalid */
.ng-untouchedwill not be replaced by
.ng-toucheduntil the user leaves the input for the first time
For templating purposes, Angular also gives you access to the unprefixed properties on the input, in both code and template:
<input name="myInput" [formControl]="myCustomInput">
<div [hidden]="myCustomInput.pristine">I've been changed</div>