gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 23:48:34 25 4
gpt4 key购买 nike

下面的代码是我尝试验证表单中的值列表。这些值位于 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:如何验证 ng-repeat 表单字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28394496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com