我有一个Angular(使用最新版本的Angular 7)formgroup与嵌套表单组:
this.form = this._formBuilder.group({
person: this._formBuilder.group({
name: [''. Validators.required],
firstName: [''. Validators.required],
birthDate: [new Date()]
}),
request: this._formBuilder.group({
title: [''. Validators.required],
startDate: [new Date()],
endDate: [new Date()],
}),
});
这需要映射到具有不同结构的API POST模型:
interface ApiData {
personName: string;
personFirstName: string;
personBirthDate: Date;
requestTitle: string;
requestStartDate: Date;
requestEndDate: Date;
}
目前我有一个在init上调用的_patchForm方法,如果表单用作编辑表单(如果id在路由中),则将API数据转换为表单模型:在表单提交上我将表单模型转换为API数据 .
let entity: ApiData = {};
entity.personName = this.form.get('person').get('name').value;
entity.personFirstName = this.form.get('person').get('firstName').value;
...
this._http.post(apiUrl, entity).subscribe( ... )
在C#中我使用Automapper,但由于不是为Angular编写的automapper-ts,我不确定这是否适用于FormGroup模型 .
这是可能的,还是有另一种更多的Angular方法来解决这个问题?我正在我的应用程序上的表单上进行这种类型的转换 .