根据API文档,更改嵌套值的正确方法是使用
patchValue
方法。但是如何更改嵌套数组中的值,例如此示例中的汽车列表。如何获取list数组的第一项并将model
更改为Fiesta
? Plunker
myForm.patchValue({'list': 0: {'model': 'Fiesta'});
无效。@Component({
moduleId: __moduleName,
selector: 'my-app',
template: `<div><pre>{{ myForm.value | json }}</pre></div>`
})
export class AppComponent {
public myForm: FormGroup;
constructor(@Inject(FormBuilder) private _fb: FormBuilder) {
this.myForm = this._fb.group({
name: 'Cars',
list: this._fb.array([
this.initCar(),
this.initCar(),
this.initCar()
]),
});
/** Change value Here **/
this.myForm.patchValue({name: 'Automobile'});
};
initCar() {
return this._fb.group({
automaker: 'Ford',
model: 'Fusion'
});
}
}
.at(0)
方法? - khexat(index: number) : AbstractControl
。 - edumas000