Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 child component loads before data passed with @input()

I have two component in my angular 2 application : component A (parent) and component B (child).

When i passe data (myData) from A to B with @Input() i get my data in my B (child component), but the problem is that the child component loads BEFORE myData and i got indefined, the only way a can console.log(myData) is in ngOnDestroy hook !

How to deal with this kind of load order ?

like image 852
Nacim Idjakirene Avatar asked Mar 26 '26 15:03

Nacim Idjakirene


2 Answers

Use *ngIf in your parent component to delay the initialization of the child component. You will bind the child component only if myData has a value.

<parent-component>
  <child-component [input]="myData" *ngIf="myData"></child-component>
</parent-component>
like image 80
Paul Avatar answered Mar 29 '26 22:03

Paul


You can simply use a *ngIf in your parent like

< child-component *ngIf="myData" > </child-component>
like image 29
WapShivam Avatar answered Mar 29 '26 23:03

WapShivam