我無法使用以下AngularJS表單驗證代碼填充錯誤消息。它是一個嵌套循環,我試圖驗證包含問題的下拉框以及相應的答案可以驗證。下面是一個代碼示例:AngularJS表單驗證未顯示
HTML
<form name="frm" ng-controller="Contract" novalidate>
<div>
<div ng-repeat="variable in randomQuestionList">
<div ng-repeat="(key, pair) in variable">
<select style="width:450px" ng-model="selected" ng-options="var.questionDes for var in pair"/>
<option value=""> Choose a question</option>
</select>
</div>
<label for="Answer" >Answer: </label>
<input type="text" name="answer{{$index}}" ng-model="answers[$index]" ng-maxlength="50" required></input><br>
<div ng-show="frm.answer{{$index}}.$dirty && frm.answer{{$index}}.$invalid">
<span ng-show="frm.answer{{$index}}.$error.required"> Answer is a mandatory field. Please enter the answer </span>
<span ng-show="frm.answer{{$index}}.$error.maxlength"> Maximum 50 characters are allowed </span>
</div>
</div>
</div>
</form>
JS
app.controller('Contract', ['$scope', function($scope) {
$scope.answers = [];
$scope.randomQuestionList =
[
{
"questionList":
[
{
"questionDes": "What is the first name of the person you first kissed?",
},
{
"questionDes": "Where were you when you had your first kiss?",
}
]
},
{
"questionList":
[
{
"questionDes": "What was the name of your elementary/primary school?",
},
{
"questionDes": "What was your favorite place to visit as a child?",
}
]
}
];
}]);
你又能做出的jsfiddle複製的問題?和一個可能的建議:把「frm.answer {{$ index}}。$ dirty &&」放在span的ng-show中,而不是跨度之外的div,並且刪除$ invalid那個(因爲如果它導致錯誤,它將默認爲無效......其多餘) – indubitablee
請參閱plkr:http://plnkr.co/edit/0K4t8cx4rXsdH5fGrIp5?p=preview – Intrepid