我有一个这样的课程
export class Policy {
constructor() {
}
emailAddress: string;
vehicleDetails: VehicleDetails[]
}
export class VehicleDetails {
constructor(policynumber: string, vinnumber: string) {
this.policyNumber = policynumber,
this.vinNumber = vinnumber
}
policyNumber: string;
vinNumber: string;
}
现在我想在组件中实例化这样的类,这是一个提交方法,我在其中绑定表单值。但当我看到控制台时,我得到一个未定义的错误。
var policy = new Policy();
policy.emailAddress = formValue.controls.emailAddress._value;
formValue.controls.policyDetails.controls.forEach(function (x) {
policy.vehicleDetails.push(new VehicleDetails(x.controls.policyNumber._value, x.controls.vinNumber._value));
});
为什么我没有得到这个对象。
注意:我没有在@NgModule提供程序中提及这两个类。我的@NgModule是这个
@NgModule({
declarations: [
LienholderComponent,
AppComponent,
PolicyVehicleComponent
],
imports: [
RouterModule.forRoot(
APP_ROUTES,
{ enableTracing: true } // <-- debugging purposes only
),
BrowserModule,
FileUploadModule,
ReactiveFormsModule,
HttpModule
],
bootstrap: [AppComponent]
})
export class AppModule { }
答案 0 :(得分:2)
可能是因为你没有实例化你的vehicleDetails数组
应该是这样的:
export class Policy {
constructor() {
}
emailAddress: string;
vehicleDetails: VehicleDetails[] = [];
}