Update value on formArray

Viewed 2691

So I have this setup:

Model:

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

Html:

<div class="element-list">
        <form [formGroup]="survey" novalidate (ngSubmit)="onSubmit(survey)" *ngIf="items">
            <div formArrayName="sections">
                <div class="single-item" *ngFor="let section of getSections(survey); let i = index">
                    <div  [formGroupName]="i">
                        Lat: <input type="text" formControlName="lat" class="frm-txt">
                        Lon: <input type="text" formControlName="lon" class="frm-txt">
                        Long: <input type="text" formControlName="long" class="frm-txt">
                        Angle: <input type="text" formControlName="angle" class="frm-txt">
                        a: {{i.angle}}
                        <a (click)="updateData(i)">Update data</a>
                    </div>
                </div>
            </div>
        </form>
    </div>

ts:

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

By now eveything is working fine. I'm displaying the data correctly and I'm happy. The problem is whenever this line is executed:

(<FormArray>this.survey.get('sections')).at(elem.id).patchValue(elem);

It gives me this error:

ERROR TypeError: Cannot read property 'patchValue' of undefined

but its struct is like this:

enter image description here

So how can I update the formArray value at position elem.id? What I don't get, is how can be undefined if the .get should be find the 'section' data?

Thank you.

1 Answers

TL;DR - That's because you call this action before you Initialize your form.

What you're currently doing - you Initialize your form only when pointsChanged has fired. Until then, the form will remain undefined.

What you should consider doing - Initialize your form on ngOnInit or immediately when you declared your form like so:

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

When the pointsChanged is fired, consider using patchValue with the data you received.

Related