如何检查可观察数组的长度 [英] How to check the length of an Observable array
本文介绍了如何检查可观察数组的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Angular 2组件中,我有一个Observable数组
In my Angular 2 component I have an Observable array
list$: Observable<any[]>;
我的模板中有
<div *ngIf="list$.length==0">No records found.</div>
<div *ngIf="list$.length>0">
<ul>
<li *ngFor="let item of list$ | async">item.name</li>
</ul>
</div>
但是list $ .length在Observable数组的情况下不起作用.
But list$.length doesn't work with in case of Observable array.
更新:
似乎(list $ | async)?. length 给了我们长度,但是下面的代码仍然不起作用:
It seems that (list$ | async)?.length gives us the length but the below code still doesn't work:
<div>
Length: {{(list$ | async)?.length}}
<div *ngIf="(list$ | async)?.length>0">
<ul>
<li *ngFor="let item of (list$ | async)">
{{item.firstName}}
</li>
</ul>
</div>
</div>
任何人都可以指导我如何检查可观察数组的长度.
Can anyone please guide how do I check length of Observable array.
推荐答案
您可以使用| async
管道:
<div *ngIf="(list$ | async)?.length==0">No records found.</div>
更新-Angular版本6:
如果要加载CSS骨架,则可以使用它.如果数组中没有项目,它将显示css模板.如果有数据,请填写ngFor
.
If you are loading up a css Skeleton you can use this. If the array has no items it will display the css template. If there is data then fill out the ngFor
.
<ul *ngIf="(list$| async)?.length > 0; else loading">
<li *ngFor="let listItem of list$| async">
{{ listItem.text }}
</li>
</ul>
<ng-template #loading>
<p>Shows when no data, waiting for Api</p>
<loading-component></loading-component>
</ng-template>
这篇关于如何检查可观察数组的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文