Angualr2 error: cannot set value of property # <AbstractControl>, which only getter has

The form looks something like this:

<form [ngFormModel]="myForm" (ngSubmit)="update()"> <ion-label floating>First Name</ion-label> <ion-input type="text" id="fname" [ngFormControl]="fname"> </form> 

Associated class:

 export class ProfilePage { myForm: ControlGroup; fname: AbstractControl; constructor(private _profile: Profile, fb: FormBuilder) { this.myForm = fb.group({ 'fname': ['', Validators.compose([Validators.required, Validators.minLength(2), firstCharacter])] }); this.fname = this.myForm.controls['fname']; Promise.all([this._profile.firstname, this._profile.lastname, this._profile.base64Image]).then(values => { this.fname.value = values[0]; // this.lname.value = values[1]; }); } 

Received error:

 EXCEPTION: Error: Uncaught (in promise): TypeError: Cannot set property value of #<AbstractControl> which has only a getter 
+6
source share
2 answers

I think you should use setValue or patchValue methods for FormGroup.

 this.myForm.patchValue({fname: firstName}); 

use patchValue if you want to selectively update only certain fields or setValue and update all

+10
source

Try:

 (this.fname as Control).updateValue(values[0]); 
+2
source

All Articles