我在ng-repeat
中对我的texbox进行了以下验证<div class="col-md-6 col-lg-2">
<div class="form-group">
<label for="Country{{$index}}" class="control-label required">Country</label>
<div class="input-group">
<mc-lookup-dropdown data-lookup-name="CountryType" required data-model="ContactAddress.Country" id="Country{{$index}}" name="Country{{$index}}" class="form-control"></mc-lookup-dropdown>
</div>
<div data-ng-messages="memberDemographics.demographics.$error" class="validation-errors">
<div data-ng-message="Country">{{ ContactAddress.$serverErrors.Country }}</div></div>
<div data-ng-messages="demographicsForm.{{'Country'+$index}}.$error" class="validation-errors">
<div data-ng-message="required" data-ng-show="!demographicsForm.{{'Country'+$index}}.$pristine">This Field is Required</div>
</div>
</div>
</div>
在页面加载“rror:[$ parse:syntax] 上抛出以下错误
http://errors.angularjs.org/1.5.8/ $解析/语法P0 =%7B&安培; P1 =是%20not%20A%20validNaNdentifier&安培; P2 = 18&安培; P3 = demographicsForm%7B%7B'PhoneNumber'%2B%索引%7D% 。7D%24error&安培; P4 =%7B%7B'PhoneNumber'%2B%索引%7D%7D%24error“
我需要在ng-message中使用表达式,因为textname取决于ng-repeat循环的$ index ..
答案 0 :(得分:1)
这些行有错误:
<div data-ng-messages="demographicsForm.{{'Country'+$index}}.$error" class="validation-errors">
<div data-ng-message="required" data-ng-show="!demographicsForm.{{'Country'+$index}}.$pristine">This Field is Required</div>
更改为:
<div data-ng-messages="demographicsForm['Country' + $index].$error" class="validation-errors">
<div data-ng-message="required" data-ng-show="!demographicsForm['Country'+$index].$pristine">This Field is Required</div>
来自this文章
使用Angular的模板系统时,您只需要包裹大括号 - 大括号告诉Angular替换值。在这种情况下,data-ng-message的值是正在评估的表达式。
答案 1 :(得分:0)
通过将html更改为
来解决问题<div data-ng-messages="demographicsForm['Country'+$index].$error" class="validation-errors">
<div data-ng-message="required" data-ng-show="!demographicsForm['Country'+$index].$pristine">This Field is Required</div>
</div>
三江源