angular - TypeError: data is not iterable ,在嵌套数组上使用 ngfor
问题描述
rowData= [{
"label": "president",
"contestants": [{
"name": "john key",
"votes": 2
}, {
"name": "john hi",
"votes": 1
}, {
"name": "john kl",
"votes": 1
}]
}, {
"label": "organizer",
"contestants": [{
"name": "michael skie",
"votes": 3
}, {
"name": "michael e",
"votes": 1
}]
}, {
"label": "secretary",
"contestants": [{
"name": "sampson eons",
"votes": 2
}, {
"name": "sampson mji",
"votes": 2
}]
}]
<div *ngIf="rowData.length >0">
<div *ngFor="let arr of rowData let i=index">
<div *ngFor="let elm of arr.contestants">
<ngx-charts-bar-vertical
[view]="view"
[scheme]="colorScheme"
[results]="elm"
[gradient]="gradient"
[xAxis]="showXAxis"
[yAxis]="showYAxis"
[legend]="showLegend"
[showXAxisLabel]="showXAxisLabel"
[showYAxisLabel]="showYAxisLabel"
[xAxisLabel]="xAxisLabel"
[yAxisLabel]="yAxisLabel"
(select)="onSelect($event)">
</ngx-charts-bar-vertical>
</div>
</div>
</div>
我收到一个错误,即数据不可迭代。有人能帮我吗
解决方案
我通过重新排列json解决了它
rowData = [
[{
"label": "president",
"contestants": [{
"name": "john key",
"votes": 2
}, {
"name": "john hi",
"votes": 1
}, {
"name": "john kl",
"votes": 1
}]
}, {
"label": "organizer",
"contestants": [{
"name": "michael skie",
"votes": 3
}, {
"name": "michael e",
"votes": 1
}]
}, {
"label": "secretary",
"contestants": [{
"name": "sampson eons",
"votes": 2
}, {
"name": "sampson mji",
"votes": 2
}]
}]
]
<div *ngIf="rowData.length >0">
<div *ngFor="let arr of rowData let i=index">
<div *ngFor="let elm of arr.contestants">
<ngx-charts-bar-vertical
[view]="view"
[scheme]="colorScheme"
[results]="elm"
[gradient]="gradient"
[xAxis]="showXAxis"
[yAxis]="showYAxis"
[legend]="showLegend"
[showXAxisLabel]="showXAxisLabel"
[showYAxisLabel]="showYAxisLabel"
[xAxisLabel]="xAxisLabel"
[yAxisLabel]="yAxisLabel"
(select)="onSelect($event)">
</ngx-charts-bar-vertical>
</div>
</div>
</div>
我移动了结果 rowData[0]; 它解决了我的问题
推荐阅读
- protractor - 量角器:如何等待所有元素变得不可见
- java - 导出的可运行 Java 程序不适用于 main 方法
- java - 我正在尝试在我的应用程序中添加 google 登录,但此依赖项不起作用
- c# - 未使用 Dictionary 调用 ForEach 扩展方法
- mysql - SQL:我得到一个不明确的错误
- python - 如何使用熊猫只保留数字和字母?
- jquery - Q Library Promise Pattern 用于检索多个 XML 文件并按顺序显示它们
- python - Slug 字段 RecursionError
- python - 用于光谱聚类的 Python 输入
- selenium - Jenkins 构建结束后自动关闭 CMD 窗口