我正在尝试按照
AngularJS example对所需字段进行内联验证.然而,当涉及到使用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>
知道我做错了什么或我能做些什么来解决它?
不幸的是你不能这样做. input元素不喜欢动态生成名称.您将需要使用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>