I am trying to show a loading animation in Angular 10 while I am retrieving data. I can show the loading animation if I use a specific time value but I cannot get it to show just while the data is being gathered. Please see a snippet of my code below. Instead of manually calling the loading animation I would like it to only run while the 'getAllDocuments' function is running. Sometimes my hard coded time value is too short and others too long. I would like it to be correct every time.
ngOnInit() {
this.getAllDocuments();
this.triggerLoadingAnimation(12);
}
ngAfterViewInit(): void {
this.dataSource.sort = this.sort;
this.dataSource.paginator = this.paginator;
}
public triggerLoadingAnimation(num){
this.spinner.show();
setTimeout(() => {
this.spinner.hide();
}, num*1500);
}
async getAllDocuments() {
this.repoService.getData('/api/Prometheus/Documents')
.subscribe(res => {
this.dataSource.data = res as Document[];
})
}