表单看起来像:
<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
答案 0 :(得分:10)
我认为你应该使用FormGroup的setValue或patchValue方法。
this.myForm.patchValue({fname: firstName});
如果您想有选择地仅更新某些字段,或使用setValue并更新全部
,请使用patchValue答案 1 :(得分:2)
尝试:
(this.fname as Control).updateValue(values[0]);