angular - 仅当来自服务器端的数据时如何呈现 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 时才显示。
谢谢,
解决方案
您的代码非常正确。您需要;
在. 所以你的 html 将是:else
ngIf
<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>
推荐阅读
- javascript - Chrome 投射自定义接收器“投射失败。请重试”
- javascript - d3 v3 - 同步画笔缩放和滚动缩放
- git - 如何将 core.ignorecase 添加到 .gitattributes
- python - 如何让我的不和谐机器人进行计算
- r - 混淆矩阵错误(pred,as.factor(owner.df$Probability > 0.5,“owner”,:在 R 中找不到对象“pred”
- javascript - Mongo注册发生但护照给我未经授权。数据存储在MongoDB中
- javascript - 正确的节点获取 POST 的 AXIOS 版本
- mysql - mysql - 插入/更新字符串值而不用引号引起来
- javascript - 如何在下面的代码中获取 data.next() 和 data.complete() ?
- php - Laravel - 在迁移时为 unsignedInteger 获取“1067 无效默认值”