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', []); (index.html) <div ng-app="myApp"> <form name="playerForm"> <input type="text" name="playerName" ng-model="player.name"...