https://docs.angularjs.org/api/ng/type/form.FormController
在将表单验证添加到引导项目时,我发现自己经常编写这种模式:
<form name="myForm">
<div class="form-group"
ng-class="{ 'has-error' : myForm.email.$invalid && (myForm.$submitted || myForm.email.$touched) }">
<label>Email*</label>
<input type="email"
class="form-control"
name="email"
ng-model="checkout.info.customer_email"
required />
<div class="help-block"
ng-show="myForm.email.$invalid && (myForm.$submitted || myForm.email.$touched)">
<div ng-show="myForm.email.$error.required">This field is required.</div>
<div ng-show="myForm.email.$error.email">This email address is not properly formatted.</div>
</div>
</div>
</form>
我试图弄清楚是否有一种简单的方法可以将新属性添加到表单控制器的元素中,例如:
myForm.email.$showError = myForm.email.$invalid && (myForm.$submitted || myForm.email.$touched
我可以为此创建一个函数,但它并不是必须编写的正确解决方案,showError(myForm, myForm.email)
但在查看 FormController 的源代码后可能会容易得多