Angular 在formArray上更新值

Angular 在formArray上更新值,angular,typescript,angular2-forms,angular-reactive-forms,Angular,Typescript,Angular2 Forms,Angular Reactive Forms,所以我有这样的设置: 型号: export class MapDetailModel{ id: number; lat: number; lon: number; alt: number; long: number; angle: number; distance?: number; pendenza?: number; } Html: 拉丁美洲: 朗: 长: 角度: a:{{i.angle}} 那么如何更新位置elem.id

所以我有这样的设置:

型号:

export class MapDetailModel{
    id: number;
    lat: number;
    lon: number;
    alt: number;
    long: number;
    angle: number;
    distance?: number;
    pendenza?: number;
}
Html:


拉丁美洲:
朗:
长:
角度:
a:{{i.angle}}

那么如何更新位置
elem.id
处的formArray值呢?我不明白的是,如果
.get
应该找到
'section'
数据,那么如何可以取消定义


谢谢。

TL;DR-这是因为您在初始化表单之前调用了此操作

您当前正在执行的操作-只有在触发
pointsChanged
时才初始化表单。在此之前,表单将保持未定义状态

你应该考虑做的事情——在<代码> nGuniIT < />代码中初始化你的窗体,或者在<>强>声明< /强>时,你的表单像这样:

survey: FormGroup = this.fb.group({
  sections: this.fb.array([]),
});

当<代码> Posits改变被激发时,考虑使用<代码> PATCHValue<代码>所接收的数据。

@Component({
    //moduleId: module.id,
    changeDetection: ChangeDetectionStrategy.OnPush,
    selector: 'my-info-bar',
    templateUrl: 'map-info-bar.component.html',
    styleUrls: ['map-info-bar.component.css'],
    providers: [],
})
export class MapInfoBarComponent implements OnInit, OnDestroy
{
  zoomLevels: any[];
  points: MapDetailModel[] = [];
  isAlive = true;
  survey: FormGroup;
  items = false;

  constructor(
    private mapService: MapService,
    private fb: FormBuilder,
    private  changeDetectorRef: ChangeDetectorRef
    ){}

  ngOnInit() {

    this.mapService.singlePointChanged
    .takeWhile(() => this.isAlive)
    .subscribe( evt => {
      if(!!evt && !!this.survey.value.sections[0]){
        let elem;
        this.points.forEach(el => {
          if(el.id == evt.id){
            el = evt;
            elem = el;
          }
        });
        (<FormArray>this.survey.get('sections')).at(elem.id).patchValue(elem);
         this.changeDetectorRef.detectChanges();
      }
    })

    this.mapService.pointsChanged
    .takeWhile(() => this.isAlive)
    .subscribe( evt => {
      if(!!evt){
        this.points = evt;

        if(!this.items){
          this.survey = this.fb.group({
            sections: this.fb.array([
              this.initSection(0),
            ]),
          });
          this.items = true;
        } else {
          this.addSection(this.points.length - 1);
        }
      }
    });
  }

  initSection(idx: number) {
    return this.fb.group({
      lat: [this.points[idx].lat],
      lon: [this.points[idx].lon],
      long: [this.points[idx].long],
      angle: [this.points[idx].angle]
    });
  }

  addSection(idx: number) {
    const control = <FormArray>this.survey.get('sections');
    control.push(this.initSection(idx));
  }

  getSections(form) {
    return form.controls.sections.controls;
  }

  updateData(index: number){
    const values = this.survey.value.sections[index] as MapDetailModel;
    this.mapService.setChangePoint(values);
  }

  ngOnDestroy(): void {
    this.isAlive = false;
  }
}
survey: FormGroup = this.fb.group({
  sections: this.fb.array([]),
});