2017-08-11 97 views

回答

0

嵌套形式的Angular(2.x +)方法與AngularJS(1.x)不相似。

在Angular中,FormGroup s和FormArray s已經讓您創建嵌套表單。

<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那麼,我們就在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> 
+0

我很困惑,但我會嘗試和確認 – Krunal