我在表单生成器中收到此错误“无法读取未定义的属性'控件'”。
我的代码如下
我在构造函数中初始化initForm
this.initForm();
我的initForm方法
private initForm() {
this._userService.getCurrentUserProfileData(this._getProfileToUpdate())
.subscribe((user) => {
this.userFormGroup = new FormGroup({
firstName: new FormControl(user.firstName, [Validators.required, Validators.pattern('^[a-zA-Z]*$')]),
lastName: new FormControl(user.lastName, [Validators.required, Validators.pattern('^[a-zA-Z]*$')]),
email: new FormControl(user.email, [Validators.required, Validators.email]),
mobile: new FormControl(user.mobile, [Validators.required, Validators.pattern('^[0-9]*$')]),
workNumber: new FormControl(user.workNumber, [Validators.required, Validators.pattern('^[0-9]*$')]),
role: new FormControl(user.roleId, Validators.required)
});
}, (error) => {
this._errorService.openErrorPopup('Failed to get profile data.');
}
);
}
我的模特
private _getProfileToUpdate() {
return {
firstName: this.userFormGroup.controls.firstName.value,
lastName: this.userFormGroup.controls.lastName.value,
email: this.userFormGroup.controls.email.value,
mobile: this.userFormGroup.controls.mobile.value,
workNumber: this.userFormGroup.controls.workNumber.value,
roleId: this.userFormGroup.controls.role.value,
};
}
我不确定为什么会这样。我最初虽然是在显示html之前没有初始化该方法。