繁体   English   中英

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

[英]How can i store separate data from an FormArray into an normal object array in Angular

这是我的 object 包含数组站,我想在其中存储来自 routeForm、FormArray“stationsName”的数据(在我通过表单将数据添加到 stationName 数组之后)

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;
 });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM