I have a parent component passing in data to a child component from an API like so:
.ts
private loadData() {
this.data = this.apiService.getData();
}
.html
<child [data]="data | async"></child>
Then I have a child component that takes this input data and attempts to display/process it like:
export class Child implements OnInit {
private _data: Data[];
@Input()
set data(value: Data[]) {
this._data = value;
}
get data(): Data[] {
return this._data;
}
constructor() {
}
ngOnInit() {
this.processData();
}
private processData() {
const data = this.data; // this is undefined
// Do processing here
}
}
I know it is because the data is async and when it calls the ngOnInit the data has not reached that component yet but how can I load the data on init or at least update it when the async completes?