首页 > 解决方案 > 仅当来自服务器端的数据时如何呈现 html

问题描述

我正在使用 Angular 4。我正在使用一个 div 来存储我的 HTML 中未找到的数据。我希望这个 div 只有在数据不来时才显示。但是为了获取我的数据需要时间,同时数据未找到 div 也会显示,直到时间数据不显示。但我只想在数据不存在时显示。

为此,我在 component.ts 中使用以下代码:

this.allData -> In this all data is coming but taking time.

以下是 HTML 文件中的代码:

<div *ngIf="allData && allData.length > 0 else noResultFound">
This is testing data come from allData variable.
</div>
<ng-template #noResultFound>
    <h2>Sorry, no results found.</h2>
</ng-template>

我面临一个问题,假设获取数据需要 10 秒,并且 10 秒 noResultFound div 显示。我希望仅在数据长度为 0 时才显示。

谢谢,

标签: angular

解决方案


您的代码非常正确。您需要;在. 所以你的 html 将是:elsengIf

<div *ngIf="allData && allData.length > 0; else noResultFound">
This is testing data come from allData variable.
</div>
<ng-template #noResultFound>
    <h2>Sorry, no results found.</h2>
</ng-template>

推荐阅读