我已经搜索了我的问题,但问题是大多数都是复选框 . 我动态创建了一个选择框取决于用户将选择多少 . 让我们说例如我选择2并且程序将动态创建2个带有选项集的选择框 . 问题是,我可以获取数据但不是全部 . 我希望它以数组形式,因为所有选择框都包含相同的formControlName . 这里的代码 . 我希望你能帮助我们 . 谢谢
被构造函数调用:
createForm = () => {
this.requestForm = this.fb.group({
assestment: ['', Validators.required],
vehicles: new FormControl()
});
}
Template:
<ng-container *ngFor="let x of setIteratorVehicle(noOfVehicle)">
<div class="col-md-6 form-group">
<label for="plateNo">PlateNo</label>
<select class="form-control" id="plateNo" formControlName="vehicles" (change)="onVehicleSelect($event)">
<option *ngFor="let vehicle of vehicles" [ngValue]="vehicle.plateNo">{{vehicle.plateNo}}</option>
</select>
</div>
</ng-container>
Component.ts
onVehicleSelect(evt) {
const vehicle = evt.target.value;
const vehicleArray = (this.requestForm.controls['vehicles'] as FormArray).push(vehicle);
console.log(this.requestForm.controls.vehicles.value);
console.log(this.requestForm.controls.vehicles);
}
现在我只是记录它,看看车辆是否有 Value . 但它没有推动数据 . 一旦change()事件触发,它将覆盖车辆值 . 我希望你们能帮助我 . 谢谢 .