【问题标题】:In the HTML view/template of an Angular app how to wait for rendering of ngFor section?在 Angular 应用程序的 HTML 视图/模板中,如何等待 ngFor 部分的呈现?
【发布时间】:2020-06-26 20:08:50
【问题描述】:

假设这种情况:

您刚刚从数据库源接收到矩阵/表格样式数据 - 在变量中: 数据源

现在,您想在表格中呈现数据 - 例如 mat-table。 问题是您的数据字段在使用经典的 Anular: *ngFor 渲染时需要一些操作,并且渲染表格需要一些时间。

设计 HTML 模板/视图以显示文本的好方法是什么:

建筑报告...

当 *ngFor 正在渲染时。

当表格准备好时(*ngFor 已经渲染了最后一行),我们隐藏 Building report ... 文本并改为显示渲染的表格。

此模板代码只是在页面呈现时显示一个空白页面。我希望用户看到 Building report ... 的文本。

  <ng-container *ngIf="dataSource">

    <div class= "table-container mat-elevation-z8">
      <table mat-table [dataSource]="dataSource" matSort>

    <ng-container [matColumnDef]="column" 
      *ngFor="let column of colPropNames">

      <th mat-header-cell *matHeaderCellDef mat-sort-header> {{column}} </th>
      <td mat-cell *matCellDef="let element"> {{element[column]}} </td>

    </ng-container>

    <tr mat-header-row *matHeaderRowDef="displayCols; sticky: true"></tr>
    <tr mat-row *matRowDef="let row; columns: displayCols;"></tr>

      </table>

      <div class="box-paginator">
    <mat-paginator class="paginator"
        color="primary"
        [pageSizeOptions]="pageSizeOptions" 
        showFirstLastButtons>
    </mat-paginator>
      </div>
    </div>

  </ng-container>

</div>

【问题讨论】:

    标签: angular angular8 angular-material2


    【解决方案1】:

    Angular 有一个 if else feature:

    <div *ngIf="show; else elseBlock">Text to show</div>
    <ng-template #elseBlock>Alternate text while primary text is hidden</ng-template>
    

    因此您可以编辑代码以在 else block 中显示所需的文本:

    首先将此添加到您的*ngIf

    <ng-container *ngIf="dataSource; else elseBlock">
    

    并像这样显示您的文本:

     <ng-template #elseBlock>Building report ...</ng-template>
    

    【讨论】:

    • 这部分我很理解。数据源不是延迟。数据已经到了。我们有它。现在我们使用 ngFor 渲染它。 ngFor 中数据的渲染需要时间。我们正在等待该部分完成,对于该部分,我们需要显示等待消息...
    • 哦,我误会你了,看看这个链接:stackoverflow.com/questions/40930435/…
    • 还有这个,应该可以解决你的问题:stackoverflow.com/questions/36750678/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-11
    • 1970-01-01
    • 1970-01-01
    • 2020-07-01
    • 1970-01-01
    • 2012-11-25
    • 1970-01-01
    相关资源
    最近更新 更多