作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我在遍历数据时使用 *ngFor 遍历 json api,我想检查每个人,如果它没有 Family因此,使用 Angular ngIf 我想检查它是否存在并显示数据,如果不存在则显示此人的 No family。
JSON
{
"person": [
{
"name": "John",
"Childrens": [
{
"name": "maria k",
"age": "4 year"
},
{
"name": "mary k",
"age": "2 year"
},
{
"name": "jon k",
"age": "7 year"
},
],
"steps": [
"1. abcdef",
"2. 123456789",
"3. mnbvcxz."
],
"Family": [
"Somedata",
"Somedata",
"Somedata"
]
},
{
"name": "Mike",
"Childrens": [
{
"name": "Some name",
"age": "4 year"
},
{
"name": "Some name",
"age": "2 year"
}
],
"steps": [
"1. abcdef",
"2. 123456789",
"3. mnbvcxz."
],
// No Family Data here
}
}
我正在尝试这样做,但不起作用
HTML
<div *ngFor="let onePerson of person.Family">
<div *ngIf="!onePerson.Family?.length">
<p class="text-center"> Family does not Exists</p>
</div>
</div>
好的,我想显示该系列对象数组是否存在我能够使用正确的 ngFor 来显示所有内容,但是我在不存在系列的地方有空。所以我想显示消息
最佳答案
你可以这样做
<div *ngFor="let person of person.Family">
<div *ngIf="person?.Family">
<p class="text-center"> {{person}} </p>
</div>
</div>
</div>
编辑
你的 ngFor 应该是,
<div *ngFor="let personObj of person.persons">
<div *ngIf="personObj?.Family">
<p class="text-center"> {{personObj | json}} </p>
</div>
<div *ngIf="!personObj?.Family">
<p class="text-center"> Family does not Exists</p>
</div>
</div>
关于javascript - 遍历 json 以检查某些数据是否存在(如果不显示)不使用 ngFor 和 ngIf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52234176/
我是一名优秀的程序员,十分优秀!