APIドキュメントによると、ネストされた値を変更する適切な方法は、方法patchValue
ネストされたフォームの変更FormBuilderアレイ項目
myForm.patchValue({'key': {'subKey': 'newValue'}});
しかし、どのように車のリストのようなネストされた配列の値を変更する方法を使用することです以下のこの例では、どのようにのリストの最初の項目を取得する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)'メソッド? – khex
ありがとうございました。はいこの文書をチェックしてくださいhttps://angular.io/docs/ts/latest/api/forms/index/FormArray-class.html フォームの配列には、 'at( index:number):AbstractControl' – Dumas000