1

我正在使用 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];
          });
      }

结合上面的代码会使表单具有验证,但是如果一个表单通过,则所有表单都会通过,这是一个不希望的结果。如何使表单具有通过验证的独特方式?

4

1 回答 1

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>
于 2017-12-31T08:05:13.020 回答