Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 How to make setter wait to input setter to complete

Hi i have a problem with that. In my component i have a inputt setter like this :

 @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 i get two edit status with value true and they are comming in the almost same time. So right now for this component in this case the getDataFromFlex function will be called two times. I don't want the second time call so i think the getTrigg(boolean) will be a solve. And its doesn't work. So i need a bit of help from you guys. The getTrigg is default on component initiation set for true

like image 579
Przemysław Zamorski Avatar asked Jan 05 '23 19:01

Przemysław Zamorski


1 Answers

I recomend you to use the ngOnChanges interface to do this kind of communication with external components. This will ensure that you have the expected value to do what you need after this value changes:

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;
    }
}
like image 153
Fals Avatar answered Jan 13 '23 09:01

Fals