Angualr2错误:无法设置只有getter的#<abstractcontrol>的属性值

时间:2016-08-07 01:45:44

标签: angular

表单看起来像:

    <form [ngFormModel]="myForm" (ngSubmit)="update()">

                <ion-label floating>First Name</ion-label>
                <ion-input type="text" id="fname" [ngFormControl]="fname"> 

   </form>

相关课程:

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

        });
    }

收到错误:

EXCEPTION: Error: Uncaught (in promise): TypeError: Cannot set property value of #<AbstractControl> which has only a getter

2 个答案:

答案 0 :(得分:10)

我认为你应该使用FormGroup的setValue或patchValue方法。

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

如果您想有选择地仅更新某些字段,或使用setValue并更新全部

,请使用patchValue

答案 1 :(得分:2)

尝试:

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