According to the API documentation the proper way to change nested values is to use method patchValue
myForm.patchValue({'key': {'subKey': 'newValue'}});
But how to change values in nested arrays like list of cars in this example below. How to get first item of list array an change model
to Fiesta
? Plunker
myForm.patchValue({'list': 0: {'model': 'Fiesta'});
is not working.
@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'
});
}
}