我正在尝试从我的 api 获取数据,获取数据的代码:
getList() {
this.fileList = [];
this._globalService.getUserFiles(this.userId)
.then(files => {
files.forEach(retFile => {
this._globalService.getFileActions(retFile._id)
.then(retActions => {
this.fileList.push( {
file: retFile,
action: retActions
});
});
});
})
.finally(() => {
this.finish = true;
});
}
调用 OnInit Hook 。 之后,我的观点试图表明:
<ng-container *ngIf="finish; else wait">
<ng-container *ngIf="fileList.length; else noItems">
<li *ngFor="let item of fileList">
...
</li>
<ng-container>
<ng-template #noItems>
<span class="noItems">Any file</span>
</ng-template>
<ng-container>
效果很好。 但我的问题持续了几秒钟,#noItems 模板是显示数据何时存在,如本例所示:
最佳答案
你总是将结果插入 this.FileList,你的 this.FileList 永远不会为空,因此你的 noItem 模板永远不会显示。
关于javascript - Angular 异步数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59803344/