将 ng-repeat 对象变量绑定到 HTML 中的变量
Bind ng-repeat object variable to variable in HTML
我想就这个问题寻求帮助。
我有一个名为 testForm
的表格,其中包含一个 ng-repeat="question in questions"
。 question
有一个 QuestionId
。在这种形式下,我正在尝试应用验证。由于 questions
是动态的,我无法为输入字段使用静态名称,因此我将 question.QuestionId
作为名称应用于每个字段以使其唯一。
现在验证要求我检查字段名称。因此,例如检查 testForm
+ 字段名称 (QuestionId
)。我尝试了几种不同的方法,但我不知道如何将 question.QuestionId
添加到 testForm
。
代码:
<div ng-form="testForm" novalidate>
<div ng-repeat="question in questions">
<label class="item item-input" ng-class="{ 'has-error': testForm.question.QuestionId.$error }">
<input class="form-error border"
type="text"
ng-model="childName"
name="{{question.QuestionId}}"
ng-minlength="2"
ng-maxlength="99"
ng-required="true">
</label>
</div>
<div ng-show="testForm.question.QuestionId.$error"> asd </div>
</div>
我正在尝试进行验证,但 ng-show
将在 testForm
中查找变量 question
。如何先将question.QuestionId
转成文字,再转成testForm
?
question.QuestionId
在 testForm
对象中创建 formField。之后,您可以通过 testForm
表单对象中的键检索 formField
。
ng-show="testForm[myQuestionId].$error"
Note: You can't get question
item outside ng-repeat
, so better you can pass particular questionId
in testForm
object to get particular formField.
我想就这个问题寻求帮助。
我有一个名为 testForm
的表格,其中包含一个 ng-repeat="question in questions"
。 question
有一个 QuestionId
。在这种形式下,我正在尝试应用验证。由于 questions
是动态的,我无法为输入字段使用静态名称,因此我将 question.QuestionId
作为名称应用于每个字段以使其唯一。
现在验证要求我检查字段名称。因此,例如检查 testForm
+ 字段名称 (QuestionId
)。我尝试了几种不同的方法,但我不知道如何将 question.QuestionId
添加到 testForm
。
代码:
<div ng-form="testForm" novalidate>
<div ng-repeat="question in questions">
<label class="item item-input" ng-class="{ 'has-error': testForm.question.QuestionId.$error }">
<input class="form-error border"
type="text"
ng-model="childName"
name="{{question.QuestionId}}"
ng-minlength="2"
ng-maxlength="99"
ng-required="true">
</label>
</div>
<div ng-show="testForm.question.QuestionId.$error"> asd </div>
</div>
我正在尝试进行验证,但 ng-show
将在 testForm
中查找变量 question
。如何先将question.QuestionId
转成文字,再转成testForm
?
question.QuestionId
在 testForm
对象中创建 formField。之后,您可以通过 testForm
表单对象中的键检索 formField
。
ng-show="testForm[myQuestionId].$error"
Note: You can't get
question
item outsideng-repeat
, so better you can pass particularquestionId
intestForm
object to get particular formField.