我使用Ionic,我使用ng-repeat进行多种形式。我希望对每个表单进行单独验证,但我不确定如何才能完成这项工作。
代码:
<ion-list ng-if="question.QuestionType == 'QuestionRadio'">
<ion-radio ng-repeat="choice in question.Answers"
ng-model="vm.answer[choice.AnswerId]"
name="{{question.QuestionId}}"
ng-required="vm.answer"
ng-required="!vm.someSelected(vm.answer)">
{{choice.Text}}
</ion-radio>
</ion-list>
显示错误消息的代码:
<div class="form-errors"
ng-show="testForm[question.QuestionId].$error.required"
ng-messages="testForm[question.QuestionId].$error"
ng-messages-include="shared/form-errors.html"
ng-if="question.QuestionType == 'QuestionRadio'">
</div>
使验证工作的功能,但不适用于单独的表格:
vm.someSelected = function (object) {
return Object.keys(object).some(function (key) {
return object[key];
});
}
组合上面的代码会使表单得到验证,但是如果一个表单通过,则所有表单都会通过,这是一个不希望的结果。如何使表格具有传递验证的独特方式?
答案 0 :(得分:1)
忽略元素上的重复属性:
<ion-list ng-if="question.QuestionType == 'QuestionRadio'">
<ion-radio ng-repeat="choice in question.Answers"
ng-model="vm.answer[choice.AnswerId]"
name="{{question.QuestionId}}"
̶n̶g̶-̶r̶e̶q̶u̶i̶r̶e̶d̶=̶"̶v̶m̶.̶a̶n̶s̶w̶e̶r̶"̶
̶n̶g̶-̶r̶e̶q̶u̶i̶r̶e̶d̶=̶"̶!̶v̶m̶.̶s̶o̶m̶e̶S̶e̶l̶e̶c̶t̶e̶d̶(̶v̶m̶.̶a̶n̶s̶w̶e̶r̶)̶"̶
ng-required="vm.answer[choice.AnswerId] && !vm.someSelected(vm.answer)">
{{choice.Text}}
</ion-radio>
</ion-list>