2017-05-31 57 views
0

我使用的是NG-重复渲染形式的一个字段:绑定NG-消息NG-重复

<fieldset data-ng-repeat="education in edit_profile.education"> 
    <div class="row">        
    <md-input-container class="col-xs-12 col-sm-12 col-lg-6 input-profile"> 
     <label>University</label> 
     <input required="" name="editProfile_StudentUni{{$index}}" ng-model="education.uni"> 
     <div ng-messages="myProfile['editProfile_StudentUni' + $index].$error"> 
     <div ng-message="required">This is required.</div> 
     </div> 
    </md-input-container> 
    <md-input-container class="col-xs-12 col-sm-12 col-lg-6 input-profile"> 
     <label>Course</label> 
     <input required="" name="editProfile_StudentCourse{{$index}}" ng-model="education.course"> 
     <div ng-messages="myProfile['editProfile_StudentCourse' + $index].$error"> 
     <div ng-message="required">This is required.</div> 
     </div> 
    </md-input-container> 
    </div> 
    <div class="row">        
    <md-input-container class="col-xs-12 col-sm-12 col-lg-4 input-profile"> 
     <label>Start year</label> 
     <input name="editProfile_StudentStartYear{{$index}}" ng-model="education.start_year" required ng-pattern="/^20[0-9]{2}$/" md-maxlength="4"> 
     <div ng-messages="myProfile['editProfile_StudentStartYear' + $index].$error"> 
     <div ng-message="required">This is required.</div> 
     <div ng-message="pattern" class="my-message">Must be a valid year.</div> 
     <div ng-message="md-maxlength">Must be a valid year.</div> 
     </div> 
    </md-input-container> 
    <md-input-container class="col-xs-12 col-sm-12 col-lg-4 input-profile"> 
     <label>End year</label> 
     <input name="editProfile_StudentEndYear{{$index}}" ng-model="education.end_year" ng-pattern="/^20[0-9]{2}$/" md-maxlength="4"> 
     <div ng-messages="myProfile['editProfile_StudentEndYear' + $index].$error"> 
     <!-- <div ng-message="required">This is required.</div>i dont have end year required: student may still be doing the course--> 
     <div id="error5" class="error-message" ng-show="errorMsg5">{{errorMsg5}}</div> 
     <div ng-message="pattern" class="my-message">Must be a valid year.</div> 
     <div ng-message="md-maxlength">Must be a valid year.</div> 
     </div> 
    </md-input-container> 
    <md-input-container class="col-xs-12 col-sm-12 col-lg-4 input-profile"> 
     <label>Average</label> 
     <input name="editProfile_StudentAverage{{$index}}" ng-model="education.average" ng-pattern="/^[1-2]{1}[0-9]{1}(\.[0-9]{1,2})?$/" step="0.01" required/> 
     <div ng-messages="myProfile['editProfile_StudentAverage' + $index].$error"> 
     <div id="error4" class="error-message" ng-show="errorMsg4">{{errorMsg4}}</div> 
     <div ng-message="required">This is required.</div> 
     <div ng-message="pattern" class="my-message">Must be a valid value.</div> 
     </div> 
    </md-input-container> 
    </div> 
    <a class="btn remove-info" ng-show="!$first" ng-click="removeEducation(education)">Remove</a> 
</fieldset> 

的问题是,这些消息只在div的第一个副本渲染(因为该变量已经在控制器中设置)。 对于剩余的副本,输入为红色,因为它没有设置,这是正确的,但错误消息不出现。

当我检查输入元素,它显示

<input name="editProfile_StudentEndYear0" 

这是正确的,但是当我检查的消息元素,它显示

<div ng-messages="myProfile['editProfile_StudentEndYear' + $index].$error" 

我在这里看到了很多的答案是说要使用这种方法,但对我来说这是行不通的。有任何想法吗?

+0

你是什么意思的“消息只呈现div的第一个副本”?你的意思是对于每个输入,只显示第一个错误? –

+0

@bugs_cena只显示第一个输入块 –

+0

是否是'

'或''定义的元素?我假设myProfile是表单的名称。如我错了请纠正我。 –

回答

0

这句法在此plunker做工精细:https://plnkr.co/edit/3mTPRAcNmBVHdABMuZEo?p=preview

<form name="myForm" novalidate=""> 
    <div ng-repeat="fieldSet in fieldSets"> 

    <input type="text" required="" name="firstName{{$index}}" ng-model="fieldSet.firstName" /> 
    <div ng-messages="myForm['firstName'+$index].$error"> 
     <div ng-message="required"> 
     This field is required 
     </div> 
    </div> 

    </div> 
</form> 

您使用的是真正的老版本AngularJS的?

+0

我正在使用Angular 1.6.3 –