Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ngOnInit is faster than @Input

I have a situation that i m passing a value from a father component to child component with @Input. The problem is that i dont get the value from father before ngOnInit() triggered in the child component. So what happens is that when ngOnInit() tries to do sg with the value its "undefined", and a second later the value filled but ngOnInit() already done. I hope it was clear enough.

Is there any way to kind of synchronice this two so, first i get the data than fill the elements ?

         @Input('data')
      set data(value) {
        this._data = value;
         this.noActivityData = (this._data === null || this._data === undefined);
      }

ngOnInit() {

     if(this._data){
       elements.activeTimeDistribution.selected = !this.noActivityData;
       elements.stepsDay.selected = !this.noActivityData;
       elements.walkingSpeed.selected = !this.noActivityData;
       elements.activeTimeDistribution.disabled = this.noActivityData;
       elements.stepsDay.disabled = this.noActivityData;
       elements.walkingSpeed.disabled = this.noActivityData;

     }

    });
  }
like image 862
Tolga Tamer Avatar asked Nov 19 '25 14:11

Tolga Tamer


1 Answers

One option would be to add *ngIf to your child component and only render it once the parent gets the value you want to pass through

<app-child-component
*ngIf="variable_you_want_to_pass_through">
</app-child-component>

You could also use ngOnChanges, and run whatever you want to run in the ngOnInit method in here, as this will fire if the parent value changes.

https://angular.io/api/core/OnChanges

like image 139
Sam Avatar answered Nov 21 '25 07:11

Sam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!