I am experiencing an issue where certain HTML content does not load until the component has received data from an API call through a service.
Below is the relevant code snippet:
import { ApiService } from './services/api.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit, OnDestroy {
thedata;
subscription: Subscription;
constructor(private apiService: ApiService) {}
ngOnInit() {
this.getData();
}
ngOnDestroy() {
this.subscription.unsubscribe();
}
getData() {
this.apiService.getUsers().subscribe(
(res) => {
this.thedata = [res];
},
(err) => {
console.log('There was an error: ' + err);
}
)
}
}
Then in the HTML file:
<div *ngFor="let data of thedata">
<!-- other elements here and below -->
{{ data.name }}
</div>
The issue I'm facing is that the visual elements do not render until the data has finished loading. Is there a way to display the HTML content while the data is still being fetched from the API?