angular - 反应形式角度添加新控件
问题描述
我正在尝试在数组中的表单上添加新的表单控件,但我遇到的问题是我总是空控件,即使我在数组中有一些值
这就是我现在所拥有的
this.userVehicles= [];
this.userVehicles= [{Model: 'Fiat', RegistrationPlate: 'Taxi', LastServiceDate: 'Nov 11', Vin: '111', YearManufacture: '2015'}];
const vehicleFGs: any = this.userVehicles.map(vehicle => this._fb.group({
Model: [vehicle.model],
RegistrationPlate: [vehicle.registrationPlate],
LastServiceDate: [vehicle.lastServiceDate],
Vin: [vehicle.vin],
YearManufacture: [vehicle.yearManufacture],
}));
const vehicleFormArray: FormArray = this._fb.array(vehicleFGs);
((this.myAccountForm as FormGroup).get('Owner') as FormGroup).setControl('Vehicles', vehicleFormArray);
我认为我遇到的问题是在这一行
((this.myAccountForm as FormGroup).get('Owner') as FormGroup).setControl('Vehicles', vehicleFormArray);
我想我没有正确绑定控件,知道吗?
解决方案
而不是使用.setControl()
,使用.addControl()
:
this.form = this.formBuilder.group({
owner: this.formBuilder.group({}),
});
const vehicleFormGroups: FormGroup[] = this.vehicles.map(v => {
return this.formBuilder.group({
model: [
v.Model,
],
registrationPlate: [
v.RegistrationPlate,
],
lastServiceData: [
v.LastServiceDate,
],
vin: [
v.Vin,
],
yearManufacture: [
v.YearManufacture,
],
});
});
const vehiclesFormArray: FormArray = new FormArray(vehicleFormGroups);
(this.form.get('owner') as FormGroup).addControl('vehicles', vehiclesFormArray);
推荐阅读
- python - 双索引 Pandas 沿两个索引之一移动
- c# - ASP.NET 启用 Cors 但在使用 Angular 访问 API 时 Chrome 中仍然出现 cors 错误
- asp.net - 如何访问转发器中的 XML 子节点
- javascript - JavaScript 以错误的顺序追加/写入字符串
- c++ - objdump 不显示机器码,但显示 asm
- c# - 更新 WPF 桌面桥通用应用程序时无法升级设置
- android-studio - 在网络共享上创建新的 android studio 项目时如何修复“路径...不可写”
- python-3.x - loc 在 pandas 中无法正常运行
- java - 从两个不同的类有条件地调用具有相同名称的方法
- azure-devops - 用于获取 Azure DevOps 中项目的所有可用任务/错误等的 API