【问题标题】:when waiting for response show reload icon using Angular等待响应时使用 Angular 显示重新加载图标
【发布时间】:2019-02-17 22:24:31
【问题描述】:

我有这个 Typescript 代码,可以从后端获取所有数据。

getalleventsserial() {
    this.activatedRoute.params.subscribe(
      params => {
        this.ns.eventsbyserial(params['id']).subscribe(
          notificcationserial => {
            this.notif = notificcationserial;
            this.isEmpty = (Array.isArray(this.notif)) ? true : false;
          }
        );
      }
    );
  }

在这段 html 代码中,我可以显示数据或仅显示消息。

<div style="text-align:center; color:#EE6E73;">
    <p *ngIf="isEmpty">Eventes for this product are: </p>
    <h5 *ngIf="!isEmpty">Events are empty!</h5>
  </div>
<div class="grid-container">
  <table *ngFor="let item of notif">
  <td>Date: {{item.dt}}</td>
  <td>Name: {{item.name}}</td>
  </table>
</div>

在这段代码中,我遇到了一个问题:当我从服务中获取更多数据时,显示此&lt;h5 *ngIf="!isEmpty"&gt;Events are empty!&lt;/h5&gt; 一段时间,最后数据显示在视图中。你有什么想法,如何解决这个问题,或者如何等待数据,在 html 显示重新加载图标。数据到达时图标隐藏和显示数据,还是在响应为空时显示此消息?点赞this

【问题讨论】:

    标签: angular typescript reload


    【解决方案1】:
    // *.component.ts
    ...
    this.isLoading = true;
    this.ns.eventsbyserial(params['id']).subscribe(
          notificcationserial => {
            this.notif = notificcationserial;
            this.isEmpty = (Array.isArray(this.notif)) ? true : false;
            this.isLoading = false;
          }
        );
    
    //*.component.html
    <ng-container *ngIf="isLoading">Loading... or your custom Loader</ng-container>
    

    这可以帮助您显示加载程序、错误消息(如果存在)。希望这会有所帮助。

    【讨论】:

    • 是的,但我想在第一次加载时显示,而不是同时显示。并在过去显示消息,空数据或数据。谢谢!
    • 这将按预期工作。 1. 显示装载机。 2. 收到服务器的响应后,判断 isEmpty 是真还是假。 3.隐藏加载器并显示数组内容或显示Events are empty
    猜你喜欢
    • 2013-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多