I am struggling with AngularJS validation. Here's the SSCCE in HTML 5 (which due to my server structure must be valid XML):
View in JSFiddle
HTML
<div ng-app=""> <form name="myForm" ng-controller="ValidationTest" novalidate="novalidate"> <input name="email" ng-model="myForm.email" type="email" required="required" /> <div> Field: <span ng-show="myForm.email.$valid">valid</span> <span ng-show="myForm.email.$invalid">invalid</span> <span ng-show="myForm.email.$pristine">pristine</span> <span ng-show="myForm.email.$dirty">dirty</span> </div> <div> Form: <span ng-show="myForm.$valid">valid</span> <span ng-show="myForm.$invalid">invalid</span> <span ng-show="myForm.$pristine">pristine</span> <span ng-show="myForm.$dirty">dirty</span> </div> </form> </div>
CSS
input.ng-valid { border-color: #0f0; } input.ng-invalid { border-color: #f00; } input.ng-pristine { background-color: #eef; } input.ng-dirty { background-color: #fee; }
Javascript
function ValidationTest($scope) {}
When I run this script, the CSS classes applied to the form element correctly reflect its validation status, as do the ng-show directives for the form. However, the ng-show directives for the field start correctly (shown as "invalid" and "untouched"), but they all disappear as soon as I start typing in the field. As far as I can tell, I am doing this, as the documentation shows, and what is wrong?
source share