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

前端之家收集整理的这篇文章主要介绍了angularjs – 循环遍历表单内所有ng-form实例以进行验证前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我创建了一个 plunker.

当单击“名称”按钮时,我会动态生成输入元素,每个都在ng-form内.

如何获取ng-form的所有实例以验证每个ng-form?如果字段无效或创建了新的ng-form,那么“Name”按钮将保持禁用状态?

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

由于简洁原因,我删除了表单中的其他字段.它基本上是一个长形式,类似于

<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)如果你想/需要在主窗体中引用子窗体控件,你需要使用自定义指令.

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

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

猜你在找的Angularjs相关文章