1 回答

TA貢獻(xiàn)1982條經(jīng)驗 獲得超2個贊
在渲染頁面之前需要等待接收到的數(shù)據(jù)。你可以做兩件事:
使用布爾值和ngIf指令:
loadingData = true;
ngOnInit() {
this._assingedSiteService.getAssignedSitesForLogInUser().subscribe((res) => {
this.sites = res;
console.log(this.sites);
this.loadingData = false;
}, (error) => {
console.log(error);
}
);
}
模板
<select class="form-control" *ngIf="!loadingData">
<option *ngFor="let site of sites">
{{site.siteName | json}}
</option>
</select>
我更喜歡,如果您的訂閱中沒有邏輯,請async在模板中使用管道:
sites$: Observable<Site>;
ngOnInit() {
this.sites$ = this._assingedSiteService.getAssignedSitesForLogInUser();
}
模板:
<select class="form-control">
<option *ngFor="let site of sites$ | async">
{{site.siteName | json}}
</option>
</select>
添加回答
舉報