我使用以下代码检查数组是否为空,然后显示消息,否则显示列表。但是它只显示列表正在工作。没有显示“无消息”。怎么了
<ion-row *ngFor="let item of globalArray">
<div *ngIf="!globalArray?.length > 0">
<p>No messages</p>
</div>
<div *ngIf="globalArray?.length > 0">
<ion-item>
{{item.message}}
</ion-item>
</div>
</ion-row>
因为在你的数组中
<ion-row *ngFor="let item of globalArray">
<div *ngIf="!globalArray?.length > 0">
<p>No messages</p>
</div>
<div *ngIf="globalArray?.length > 0">
<ion-item>
{{item.message}}
</ion-item>
</div>
</ion-row>
您将直接迭代并检查ngFor内部数组的长度,该数组将永远不会执行。您需要检查ngFor之外的长度或在迭代数组之前。您也可以从ts文件中进行检查,也可以在视图中进行处理。
在您看来,您可以按照以下方式处理:
<div *ngIf="!globalArray || globalArray.length === 0">
<p>No messages</p>
</div>
<div *ngIf="globalArray || globalArray.length > 0">
<ion-row *ngFor="let item of globalArray">
<ion-item>
{{item.message}}
</ion-item>
</ion-row>
</div>
在.ts文件中,您可以类似地检查数组长度并在此基础上进行变量切换,并在两个Div上将ngIf与该变量一起使用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句