我想遍历我的 json 文件中的对象数组。
JSON
[
{
"name": "Mike",
"colors": [
{"name": "blue"},
{"name": "white"}
]
},
{
"name": "Phoebe",
"colors": [
{"name": "red"},
{"name": "yellow"}
]
}
]
html
<div *ngFor="let person of persons">
<p>{{person.name}}</p> <!-- this works fine-->
<p *ngFor="let color of person.colors"> <!--I want to list the colors of the person here-->
{{color.name}}
</p>
</div>
我无法访问一个人的颜色数组。我该如何实现?
我已经看过这些帖子,但它们的解决方案对我没有帮助:
最佳答案
对于 Angular 6.1+ ,您可以使用默认管道 keyvalue
( Do review also ) :
<ul>
<li *ngFor="let recipient of map | keyvalue">
{{recipient.key}} --> {{recipient.value}}
</li>
</ul>
以前:正如您所说:
Angular2 - *ngFor / loop through json object with array ,这帮不了你
你不需要那个,因为你的代码工作正常,请检查
关于arrays - 使用 *ngFor (Angular 4) 遍历 JSON 对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48402215/