如何将FormArray中的单独数据存储到Angular中的普通对象数组中



这是我的对象,包含阵列站,我想在其中存储来自routeForm的数据,FormArray"stationsName"(在我通过表单将数据添加到stationsName数组之后(

Routes = {
name : '',
departureDate: '',
arrivalDate: '',
stations: [{
name: '',
arrival: ''
}]
}
routeForm: FormGroup;
ngOnInit() {
this.routeForm = this.formBuilder.group({
stationsName: new FormArray([])
});

get f() {
return this.routeForm.controls;
}
get stationsName() {
return this.f.stationsName as FormArray;
}
addNewStationName() {
this.stationsName.push(
this.formBuilder.group({
name: ['', []],
arrival: ['', []]
})
);
}

我怎么能做这样的事情:

this.Routes.stations[0].name = this.routeForm.stationsName[0].name.value
this.Routes.stations[0].arrival = this.routeForm.stationsname[0].arrival.value
ngOnInit() {
this.routeForm = this.formBuilder.group({
stationsName: new FormArray([])
});
this.routeForm.valueChanges.subscribe(data=>{
this.Routes.stations = data.stationsName;
});
}

最新更新