2017-05-12 30 views
3

我使用複雜的FormBuilder構建表單。Angularjs 2/4使用Complex Formbuilder進行表單驗證

this.myForm = this._fb.group({ 
     name: ['', [Validators.required, Validators.minLength(5)]], 
     addresses: this._fb.group({ 
      street: ['pp', Validators.required], 
      state: { 
      city: ['New York'] 
      }, 
      postcode: [''] 
     }) 
}); 

在嵌套的形式中,我有一個可以更新城市的字段。我怎樣才能使用formControlName。

Plunkr

回答

3

您只需打state作爲formGroup了,裏面的表單控件city

this.myForm = this._fb.group({ 
     name: ['', [Validators.required, Validators.minLength(5)]], 
     addresses: this._fb.group({ 
      street: ['pp', Validators.required], 
      state: this._fb.group({ 
      city: ['New York'] 
      }), 
      postcode: [''] 
     }) 
    }); 

模板:

<div formGroupName="state"> 
    <input formControlName="city"/> 
</div> 

你分叉

Plunker