angular - 如何使用角度 11 在 for 循环中使用数组对象?
问题描述
我从后端 API 获取数据,需要在前端循环数据但其值不返回。
这是我来自后端 API 的回复。
[
[
{
"choice": 1,
"count": 4,
"percentageValue": "80.0000"
},
{
"choice": 2,
"count": 1,
"percentageValue": "20.0000"
}
],
[
{
"choice": 1,
"count": 4,
"percentageValue": "80.0000"
},
{
"choice": 2,
"count": 1,
"percentageValue": "20.0000"
}
]
]
在 ts 文件中
submitPoll(data){
this.pollService.submitPoll(data).subscribe(response =>{
this.percentage=response.body;
});
}
在 html 文件中
<p *ngFor="let percentage of percentage;let i= index;">
{{percentage.choice }}
</p>
如何为数组对象使用 for 循环?我需要在选项中显示百分比
解决方案
正如@MichaelD 提到的,percentage
是数组数组。一个简单的解决方案是使用*ngFor
两次。
<div *ngFor="let innerArray of percentage;">
<p *ngFor="let obj of innerArray;">
{{obj.choice}}
</p>
</div>
推荐阅读
- flutter - Flutter + Provider + Sqflite:聚合数据状态管理
- python - Python Webelement 到 .text selenium
- python - 使用 K-means 对数据集中的热门主题进行聚类
- c# - 加载正确的页面以运行 C# MVC
- typescript - 从不同事件触发的 lambda 的打字稿语法
- tensorflow - 使用 Keras 预训练的视网膜网络模型。SavedModel 文件不存在于:“路径”/“文件”.h5/{saved_model.pbtxt|saved_model.pb}
- php - 是否可以一次获取数据并对其执行查询而不是 laravel 中的数据库?
- python - 从基于登录的网站抓取数据时如何避免被禁止?
- python - 使用 python 保存不同的预设
- r - 当行未按顺序排序时,如何创建表示行号的变量?