Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angualr2 Error : Cannot set property value of #<AbstractControl> which has only a getter

Tags:

angular

Form looks something like:

    <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];

        });
    }

Error received:

EXCEPTION: Error: Uncaught (in promise): TypeError: Cannot set property value of #<AbstractControl> which has only a getter
like image 674
runtimeZero Avatar asked Aug 07 '16 01:08

runtimeZero


2 Answers

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

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

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

like image 124
Saji Avatar answered Oct 03 '22 01:10

Saji


Try:

(this.fname as Control).updateValue(values[0]);
like image 40
Harry Ninh Avatar answered Oct 03 '22 02:10

Harry Ninh