簡體   English   中英

表單驗證-驗證由ng-repeat動態創建的輸入

[英]form validation- validate inputs dynamically created by ng-repeat

我正在嘗試使用Angular進行表單驗證。 我可以按以下方式驗證輸入元素,

<tr ng-repeat="answer in answers track by $index" ng-form="subQuestionForm">
    <td>
        <div>
            <div ng-class="{ 'has-error' :((subQuestionForm.ansText.$invalid && isSubmitted) || ((subQuestionForm.ansText.$invalid && subQuestionForm.ansText.$dirty))}">
                <input name="ansText" id="ansText" type='text' ng-model="answer.ansText" class="form-control" required />
                <div class="error,help-block col-md-offset-4" ng-show="((subQuestionForm.lhsText.$dirty  || isSubmitted) && ( subQuestionForm.lhsText.$invalid))">
                    <h6 class="help-block" ng-if="subQuestionForm.lhsText.$error.required">Enter  answer</h6>
                </div>
            </div>
        </div>
    </td>
</tr>

這將驗證表單,使用戶必須輸入所有答案(即,輸入創建的所有文本框),但是我希望用戶輸入2或更多。 如果僅輸入一個答案,則錯誤消息將顯示,否則該表格有效。 我嘗試通過為每個索引獲取索引來執行此操作,但是失敗了。 任何人都可以發光。

你可以這樣做:

<tr ng-repeat="answer in answers track by $index" ng-form="subQuestionForm">
    <td>
       <div>
        <input name="ansText" id="ansText" type='text' ng-model="answer.ansText" class="form-control" ng-required="$index == 0 || $index == 1 " />
            </div>    
    </td>
</tr>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM