Angular,获取Ng中复选框(或单选框)所需的Ng重复

Angular, getting Ng-required of checked checkbox (or radio) in ng-repeat

本文关键字:Ng 单选框 重复 获取 复选框 Angular      更新时间:2023-09-26

我想验证一个在ng repeat中有多个单选复选框的表单。如何使用单选按钮输入验证所选选项:
ng重复之外的验证工作正常。例如,当将$index切换到customDish时。问题是使用$index在ng repeat中进行验证。

所需字段应精确显示在ng重复循环内的选定单选按钮上。

这是一个plunker

[http://plnkr.co/edit/j6bswtctD0ixaQmcDrp1?p=preview][1]

请提供帮助
提前感谢

  [1]: http://plnkr.co/edit/j6bswtctD0ixaQmcDrp1?p=preview

只需将其与您的ng show进行比较即可:

ng-show="submitClicked && myOrderForm.dishQuantity.$error.required && 
($parent.checkboxSelection == $index)"

我又添加了一个条件,以确保该错误只显示在所选项目中。

另一种解决方案

 <input ng-pattern="onlyNumbers" placeholder="quantity" ng-required="$parent.checkboxSelection === {{$index}}" name="dishQuantity{{$index}}" id="dishPrice" data-ng-model="dish.quantity" type="text" class="dish-quantity" id="dishQuantity" /> 

和跨度标签

<span class="custom-label-danger" ng-show="submitClicked && myOrderForm.dishQuantity{{$index}}.$error.required ">Field Required</span>

也在工作:)