Incorporating $touched and $submitted states

Part of what makes form implementation so difficult to get exactly right is that they are highly stateful. DOM events, page history, user state, and countless other factors can all play a role in deciding what should be displayed to the user.

How to do it…

AngularJS 1.3 incorporates two more state representations into forms: $touched and $submitted.

The $touched state

Formerly, the closest thing to $touched was $pristine, which would only be unset if some input was entered into a field, but would not change if the field was merely entered and left as is. Now, $touched will be set if the field notices a focus event, even if the model value does not change. This can be done as follows:

(app.js) angular.module('myApp', ...

Get AngularJS Web Application Development Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.