javascript - 角度异步数据
问题描述
我正在尝试从我的 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 钩子。之后,我的观点试图表明:
<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 模板永远不会出现。
推荐阅读
- haskell - 封闭类型族中的种类多态性
- windows - Windows 在哪里存储其服务数据?
- javascript - Bot 在发送/编辑嵌入消息方面存在问题
- json - 如何在 MULE 3.9.0 buildpath 中排除 org.json 版本?
- android - Volley JSON Parse Foreach 循环
- mysql - 如何使用 Valentina Studio 制作主键?
- r - 从 hts2 包中获取预测区间:VaughanR0/Streamline-R: Hierarchical and Grouped Time Series
- php - SSL real_connect 返回 null 但错误代码为 0
- flutter - 颤动形状调整大小并在图像背景画布上拖动
- reportportal - 我可以将 Launch 中的详细信息导出到任何其他系统吗?是否有任何公开的 API 可以做到这一点?