我喜欢 Angular 表单验证状态的粒度,但是当你有一个包含很多字段的表单时,它会变得有点笨拙——即使是两个字段也会让你的标记变得有点模糊。采取以下示例片段:
<form novalidate name="myForm" ng-submit="myForm.$valid && vm.submit()">
<div>
<input type="text" required id="username" name="username" ng-model="vm.username" />
<div ng-show="(myForm.username.$touched || myForm.$submitted) && myForm.username.$invalid">
<!--various validation messages here-->
</div>
</div>
<div>
<input type="email" required id="email" name="email" ng-model="vm.email" />
<div ng-show="(myForm.email.$touched || myForm.$submitted) && myForm.email.$invalid">
<!--various validation messages here-->
</div>
</div>
</form>
有没有办法做更像下面的事情?
<form novalidate name="myForm" ng-submit="myForm.$valid && vm.submit()">
<div>
<input type="text" required id="username" name="username" ng-model="vm.username" />
<div ng-show="shouldShowValidationMessagesFor(myForm.username)">
<!--various validation messages here-->
</div>
</div>
<div>
<input type="email" required id="email" name="email" ng-model="vm.email" />
<div ng-show="shouldShowValidationMessagesFor(myForm.email)">
<!--various validation messages here-->
</div>
</div>
</form>