嵌套形式是否可以在角度2的动态形式中使用而不使用formbuilder

时间:2017-08-11 13:42:11

标签: angular

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

1 个答案:

答案 0 :(得分:1)

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

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

<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()

    })

})

您甚至可以通过创建getter s:

在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>