我正在尝试按照AngularJS示例对所需字段进行内联验证。但是,当使用a时ng- repeat,它似乎对我不起作用。
ng- repeat
<form name="myForm" novalidate> Me: <input required type="text" name="myName" ng-model="name" /> <span class="error" ng-show="myForm.myName.$error.required">Required!</span> <div ng-repeat="friend in friends"> Friends: <input required type="text" name="myFriend[{{$index}}]" ng-model="friend.name" /> <span class="error" ng-show="myForm.myFriend[{{$index}}].$error.required">Required</span> </div> </form>
JSFiddle
知道我做错了什么或我可以做些什么来解决吗?
不幸的是,您不能那样做。输入元素不喜欢动态生成名称。您将需要使用ng- form作为子表单并包装重复的元素。这是您的小提琴的叉子:http : //jsfiddle.net/p26VQ/
<div ng-controller="MyCtrl"> <form name="myForm" novalidate> Me: <input required type="text" name="myName" ng-model="name" /><span class="error" ng-show="myForm.myName.$error.required"> Required!</span> <div ng-repeat="friend in friends"> <ng-form name="subform{{$index}}"> Friends: <input required type="text" name="myFriend" ng-model="friend.name" /> <span class="error" ng-show="subform{{$index}}.myFriend.$error.required">Required</span> </ng-form> </div> </form> </div>