Angular 2 How to make setter wait for setter to complete

Hi, I have a problem with this. In my component, I have sett sett:

 @Input() set editStatus(status: boolean) {
    this.savedEditStatus = status;
    if (this.savedEditStatus === true && this.getTrigg === true) {

        this.getDataFromFlex("SHOWS_L").then((data: any) => {
            this.getTrigg = false;
            this.lengthDataOptions = data;
        })
    } else {
        this.getTrigg = true;
        this.lengthDataOptions = undefined;
    }

};

My problem is that I get two edit statuses with a value of true and they start almost at the same time. So, right now for this component in this case the getDataFromFlex function will be called twice. I don't need a second call, so I think getTrigg (boolean) will be the solution. And it does not work. So I need a little help you guys. GetTrigg by default to initiate the component set to true

+4
source share
1 answer

ngOnChanges . , :

import { Component, OnInit, Input, OnChanges, SimpleChange } from '@angular/core';

@Input() savedEditStatus: boolean; // change to field not getter / setter

ngOnChanges(changes: { [propKey: string]: SimpleChange }) {
    if (changes['savedEditStatus'] && changes['savedEditStatus'].currentValue === true && 
        this.getTrigg === true) {
        this.getDataFromFlex("SHOWS_L").then((data: any) => {
            this.getTrigg = false;
            this.lengthDataOptions = data;
        })
    } else {
        this.getTrigg = true;
        this.lengthDataOptions = undefined;
    }
}
+3

Source: https://habr.com/ru/post/1665867/


All Articles