gpt4 book ai didi

angularjs - 循环遍历表单内 ng-form 的所有实例以进行验证

转载 作者:行者123 更新时间:2023-12-02 22:36:40 24 4
gpt4 key购买 nike

我创建了一个plunker

当单击“+ Name”按钮时,我动态生成输入元素,每个元素都在 ng-form 内。

如何获取 ng-form 的所有实例来验证每个单独的 ng-form?这样,如果字段无效或创建了新的 ng-form,“+ 名称”按钮仍保持禁用状态?

编辑 有关表单结构的更多详细信息

为了简洁起见,我删除了表单中的其他字段。它基本上是一个长格式,类似于

<form name="myForm">
<input name="one" />
.
.
<div ng-repeat....>
<ng-form>
<input name="schoolName" />
</ng-form>
</div>
<button>+ Name</button> <!-- I cannot check for myForm.$valid here-->
<!-- since the person might not filled the rest of the below fields -->
<!-- hence the need to grab "each" "ng-form" -->
<input name="some" />
<input name="other" />
</form>

最佳答案

1) http://docs.angularjs.org/api/ng.directive:form说:

In angular forms can be nested. This means that the outer form is valid when all of the child forms are valid as well.

因此,只有当所有子表单都有效时,您的主表单才有效。

2)如果您希望/需要在主窗体中引用子 FormController,则需要使用自定义指令。

我更新您的代码:http://plnkr.co/edit/Vvz6VXJUj4lF0NR9gjjL

  • script.js 包含 2 个指令:“mainForm”和“childForm”,它们向您展示它们如何交互
  • index.html 中的“结果”部分显示“mainForm”何时生效。正如您所看到的,它证实了 Angular.js 文档的内容

关于angularjs - 循环遍历表单内 ng-form 的所有实例以进行验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18622134/

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