我正在尝试从表单中获取值,但是我一直在收到此错误。这是代码。我的formControlName是值的“名称”,并且我试图将其放在我创建的类Address类型的变量中。变量是newAddress,表单组是addressForm。这是我要尝试实现的功能,这是我的Address类。
onSubmit() {
this.newAddress.name = this.addressForm.value['name'];
console.log(this.newAddress.name);
this.addressForm.reset({
name: '',
addressLine1: '',
city: '',
state: '',
zipcode: '',
});
this.addressFormDirective.resetForm();
}
export class Address {
name: string;
addressLine1: string;
city: string;
state: string;
zipcode: string;
}
答案 0 :(得分:1)
首先在构造函数中将其初始化
this.newAddress=new Address()