AngularJS:如何验证ng-repeat表单字段

前端之家收集整理的这篇文章主要介绍了AngularJS:如何验证ng-repeat表单字段前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
下面的代码是我尝试验证表单中的值列表.这些值位于ng-repeat指令中的表中,应根据需要进行验证.任何输入将不胜感激.

<form name="form1">
  <table>
    <tr ng-repeat="param in params">
     <td>
        {{param.name}}
     </td>
     <td>
         <input name="i{{$index}}" ng-model="param.val" required />
         <span style="color:red" ng-show="form1.i{{$index}}.$dirty && form1.i{{$index}}.$invalid">
            <span ng-show="form1.i{{$index}}.required">This field is required</span>
         </span>
       </td>
     </tr>
  </table>
</form>

解决方法

我认为@SunilD’2对另一个答案的评论值得回答它自己的答案,因为使用ng-form的方法可以促进更多的验证方案(例如,突出显示表中其中一个输入无效的行)

<form name="form1">
  <table>
    <tr ng-repeat="param in params">
     <td>
        {{param.name}}
     </td>
     <td ng-form="cellForm">
         <input ng-model="param.val" required />
         <span ng-show="cellForm.$dirty && cellForm.$invalid">
            <span ng-show="cellForm.$error.required">This field is required</span>
         </span>
       </td>
     </tr>
  </table>
</form>

嵌套ng-form的有效性设置其父表单的有效性.

plunker

猜你在找的Angularjs相关文章