我正在使用帶有表單構建器的反應式表單來填充我的表單。
我這樣定義我的表單:
public myForm = this.formBuilder.group({
something: [''],
people: this.formBuilder.array([this.newPerson()]),
});
該newPerson()函式如下所示:
public newPerson(): FormGroup {
return this.formBuilder.group({
firstName: [''],
lastName: [''],
},);
}
在 OnInit() 中,我想用一些資料填充這個表單,這就是我遇到問題的地方。如果我做:
this.getPeople().patchValue([
{ firstName: 'Bla', lastName: 'Bla' },
{ firstName: 'Test', lastName: 'Test' },
]);
我只在我的表格中看到 Bla Bla 而不是 Test Test。為什么呢?
uj5u.com熱心網友回復:
this.formBuilder.array([this.newPerson()])=> 這將創建一個具有單個 formGroup 的 formArray。您必須呼叫 this.newPerson() 兩次才能創建 2 個表單組,然后呼叫 patchValue
public myForm = this.formBuilder.group({
something: [''],
people: this.formBuilder.array([this.newPerson(), this.newPerson()]),
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/367287.html
