簡體   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