angular - Angular *ngFor 在根目录中显示键和值,但不显示子数据
问题描述
我有一些看起来像这样的数据:
data = {
title: 'some title',
name: 'some name',
active: false,
extra: [
{
title: 'some data'
}
]
};
And I've displaying the key and values using:
<ul class="list-group" *ngFor="let item of data | keyvalue">
<li class="list-group-item">{{ item.key }}:{{ item.value }}</li>
</ul>
这个问题是它没有显示子数据,例如,extra: [];
我只是得到对象
我怎样才能解决这个问题?
解决方案
你可以使用这样的东西
<ul class="list-group">
<ng-container *ngFor="let item of data | keyvalue">
<li class="list-group-item" *ngIf="isNested(item.value)">
{{ item.key }}
<ul class="list-group">
<ng-container *ngFor="let nestedVal of item.value">
<li class="list-group-item" *ngFor="let nestedKeyVal of nestedVal | keyvalue">
{{ nestedKeyVal.key }}: {{ nestedKeyVal.value }}
</li>
</ng-container>
</ul>
</li>
<li class="list-group-item" *ngIf="!isNested(item.value)">
{{ item.key }}:{{ item.value }}
</li>
</ng-container>
</ul>
where isNested 可以这么简单
isNested(value) {
return typeof(value) === 'object';
}
推荐阅读
- reactjs - 如何在页面上导航而不重新加载页面
- javascript - 我有两个独立的功能,一个收集表单文本,另一个切换图像的显示。我无法从其他变量中提取值
- apache-spark - 动态循环所有列名的数据集
- python - 通过在一段文本中查找关键字来过滤数据框
- php - 如何在 PHP 中从画布中删除黑色背景?
- excel - 使用 Excel VBA 宏在 Word 中查找和替换页脚文本
- python - Multiproces 具有可迭代和可迭代参数的函数
- database - 如何保存更改?
- pandas - 使用 pyspark 将 base64 字符串转换为图像时出错
- python - 将具有 12 小时 AM/PM(无零填充)的 DateTime 转换为 24 小时格式