Angualr2错误:无法设置#< AbstractControl>的属性值.里面只有吸气剂 [英] Angualr2 Error : Cannot set property value of #<AbstractControl> which has only a getter

查看:75
本文介绍了Angualr2错误:无法设置#< AbstractControl>的属性值.里面只有吸气剂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

表单看起来像:

    <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

推荐答案

我认为您应该使用FormGroup的setValue或patchValue方法.

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

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

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

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

这篇关于Angualr2错误:无法设置#&lt; AbstractControl&gt;的属性值.里面只有吸气剂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆