對於input
元素添加到ng-repeat
內工作,該指數必須作爲name屬性的一部分。
<!-- index must be included in the name attribute --
<input name="doubleInputLeft" class="form-control ngMessageSample" type="{{inputType}}" ng-model="modelLeft" ng-minlength="2" ng-maxlength="20" required>
-->
<input name="doubleInputLeft{{index}}" class="form-control ngMessageSample" type="{{inputType}}" ng-model="modelLeft" ng-minlength="2" ng-maxlength="20" required>
<div ng-messages="form['doubleInputLeft'+index].$error" class="ngMessagesClass" ng-messages-multiple>
<div ng-message="minlength" class="ngMessageClass"> {{leftInputHeading}} must have at least 2 characters.</div>
<div ng-message="maxlength" class="ngMessageClass"> {{leftInputHeading}} must have at most 20 characters.</div>
</div>
否則,重複的元素將具有重複的名稱。
DEMO on PLNKR
請不要在'ng-'前面加上指令名稱的前綴。該前綴是爲核心指令保留的。有關更多信息,請參閱[AngularJS Wiki - 最佳實踐](https://github.com/angular/angular.js/wiki/Best-Practices)。 – georgeawg