javascript - Angular 异步数据

标签 javascript angular asynchronous

我正在尝试从我的 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 模板是显示数据何时存在,如本例所示:

enter image description here

最佳答案

你总是将结果插入 this.FileList,你的 this.FileList 永远不会为空,因此你的 noItem 模板永远不会显示。

关于javascript - Angular 异步数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59803344/

相关文章:

javascript - 如何修复错误类型错误 : "_co.news is undefined"?

html - flexbox 没有采用正确的宽度 Angular

java - Spring框架上的多线程(或异步)计算

c - Open MPI 中的屏障调用卡住(C 程序)

asynchronous - 等待取消异步工作流

javascript - 将用户散列密码存储在本地存储中?

Javascript:将日期/时间从毫秒转换为人类可读的字符串?

javascript - 当给出起始索引时,以循环方式查找数组的值

javascript - 如何调整图像大小以适应按下按钮时的容器高度?

javascript - 如果只有一个值,BehaviorSubject 会多次命中