The code below seems to work pretty well for doing basic required form validation.
The form displays a red Name is required message when the field is dirty + invalid and a Great! message if the field is dirty + valid.
But it's a mess having repeat this code for each and every field in the form:
<form name="myForm">
<div class="control-group"
ng-class="{error: myForm.name.$invalid && myForm.name.$dirty}">
<label>Name:</label>
<input type="text" name="name" ng-model="user.name" required/>
<span ng-show="myForm.name.$invalid && myForm.name.$dirty"
class="help-inline">Name is required</span>
<span ng-show="myForm.names.$valid && myForm.names.$dirty">Great!</span>
</div>
</form>
I would like to be able to specify the ng-show
and ng-class
attributes in some easier way.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…