[英]check with *ngIf, if there is an array or not (Angular 4)
我正在用* ngFor遍历一个json文件。 在遍历数据时,我想检查每个人(如果没有颜色数组)。 我用* ngIf做到这一点。
JSON格式
[
{
"name": "Peter",
"colors": [
{
"color": "blue"
},
{
"color": "yellow"
}
]
},
{
"name": "Maria"
}
// has no colors array
]
的HTML
<div *ngFor="let person of persons">
<div *ngIf=" what comes here?? ">
<p>{{person.name}} has no colors</p>
</div>
</div>
如果一个人没有颜色阵列,我该如何检查?
尝试这个
<div *ngFor="let person of persons">
<div *ngIf="person.colors && person.colors.length">
用文字,它给
如果数组存在并且其中至少包含一个元素
相反的是
<div *ngIf="!person.colors || !person.colors.length">
您可以使用猫王操作员将其缩短
<div *ngIf="!person.colors?.length">
很简单:
<div *ngIf="!person.colors">
<p>{{person.name}} has no colors</p>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.