使用ngIf或ngSwitch指令来控制组件在获取数据之前是否应该渲染。在组件中使用*ngIf或[hidden]属性,来使组件只有在获取到数据后才能正确渲染。示例代码如下:
@Component({
selector: 'my-component',
template: `
Loading data...
`
})
export class MyComponent {
public dataLoaded = false;
constructor(private dataService: DataService) {}
ngOnInit() {
// Get data from backend
this.dataService.getData().subscribe(response => {
// Once data is loaded, set dataLoaded to true
this.dataLoaded = true;
});
}
}