I am currently fetching data from a database and once the data is ready, I want to bind it to a child element using ViewChild in order to pass the object data to the child component.
The problem I'm facing is that the component is not immediately "ready," so I had to set a timeout of 2 seconds before binding.
However, this approach doesn't feel appropriate. How can I determine when the child component is actually ready?
The parent component fetches data from the DB
@ViewChild("assumps") assumps: AssumptionsComponent
getProposalAmounts() {
this.targets.getProposalAmounts().subscribe((data) => {
this.assumptions = data.assumptions;
setTimeout(() => {
this.assumps.setAssumptionsForm(this.assumptions);
}, 2000)
Inside the child component
setAssumptionsForm(obj: Assumptions) {
if (obj != null) {
this.assumptionsObj = obj;
}
If the timeout is removed, the child component is null; The error message says
Cannot read property 'setAssumptionsForm' of undefined
Thank you