【问题标题】:Is nested form possible in dynamic forms in angular 2 without using of formbuilder在不使用表单生成器的情况下,角度 2 中的动态表单是否可以嵌套表单
【发布时间】:2017-08-12 15:49:39
【问题描述】:

嵌套形式可能是反应形式,但我不知道如何以角度 2 的动态形式实现它 那么是否有可能以角度 2 的动态形式出现?

【问题讨论】:

  • 是的,这是可能的。到目前为止,您尝试过什么?
  • 数百万篇围绕 Angular2 动态表单的文章,只需用谷歌搜索即可
  • 我知道如何创建动态表单,但我想在动态表单中创建嵌套表单
  • 您可能正在寻找表单组?
  • 是的,你说得对@Vega

标签: angular


【解决方案1】:

Angular (2.x+) 的嵌套表单方法与 AngularJS (1.x) 不同。

在 Angular 中,FormGroups 和 FormArrays 已经允许您创建嵌套表单。

<form [formGroup]="fatherForm">

   <input [formControl]='fatherForm.get('firstName')'>

   <form  [formGroup]="fatherForm.get('childForm')">
       <input [formControl]='fatherForm.get('childForm.aNestedControl')'>
   </form>
</form>

然后在课堂上:

fatherForm = new FormGroup({

    firstName : new FormControl()

    childForm: new FormGroup({

        aNestedControl : new FormControl()

    })

})

您甚至可以通过创建getters 使其在 html 中更简洁:

在课堂上:

   get childForm(){
       return this.fatherForm.get('childForm')

   }

然后在 html 中:

<form [formGroup]="fatherForm">

   <input [formControl]='fatherForm.get('firstName')'>

   <form  [formGroup]="childForm">
       <input [formControl]='childForm.get('aNestedControl')'>
   </form>
</form>

【讨论】:

  • 我很困惑,但我会尝试确认
  • @Milad 我有问题;在提交表单时,我的父表单不包含子表单的值。如果我放置一个调试点,我可以看到包含所有选定值的子表单,但在提交父表单时显示空对象。知道这些值在哪里被遗漏了吗?
猜你喜欢
  • 1970-01-01
  • 2015-08-24
  • 2019-04-05
  • 2011-10-16
  • 1970-01-01
  • 2021-09-06
  • 2016-10-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多