我正在进行动态表单验证,如下所示:
<form role="form" name="myForm">
<div class="row" ng-repeat="field in fields">
<div class="form-group">
<input type="text" class="form-control" placeholder="{{field.title}}" name="formData[field.name]" ng-model="formData[field.name]" required ng-minlength=5 ng-maxlength=20>
<div class="error">
<small class="error" ng-show="myForm.formData[field.name].$error.minlength">
{{field.minValidationMessage}}
</small>
</div>
</div>
</div>
...
</form>
我的控制器如下:
$scope.fields = [{
name: 'name',
title: 'Name',
placeholder: 'Enter product name',
...
},{
name: 'code',
title: 'Code',
placeholder: 'Enter product code',
...
}]
$scope.formData = {
name: '',
code: ''
};
此处以下代码无效:
<small class="error" ng-show="myForm.formData[field.name].$error.minlength">
{{field.minValidationMessage}}
</small>
然而,如果没有ng-repeat(静态),它的工作原理如下:
<small class="error" ng-show="myForm.code.$error.minlength">
Your name is required to be at least 3 characters
</small>
如何在myForm.formData[field.name]
中引用ng-show
。
提前致谢。