My page contains two components, parent and child.
In the child I am receiving data from the server, and I save this data in a service. I want to execute a function in the parent only when the data in the service is not null, because I am using this data in the parent function.
How can I be sure that the data is already in the service before I execute the function in the parent?
Of course I want to execute the function while the page loads, as soon as possible, and I prefer to not use setTimeout function.
For example:
//child
this.jsonService.getData().subscribe(res => {
this.CommonService.userDetails = res;
}
//service
@Injectable()
export class CommonService {
public userDetails: any;
}
//parent
// execute only when the this.CommonService.userDetails != null
doSomething(){
alert(this.CommonService.userDetails)
}