如何使用NGFOR访问HTML文件中一个大对象中的多个对象的值?
在Angular中,我试图在HTML文件中使用*NGFOR
来访问嵌套对象值。对象serverdata
我尝试访问的是遵循。
{//whole object i try to loop through and access
"0": {//inner , nested object that store the data I want to access
"District_en": "Sha Tin",
"Name_en": "This will be the new name",
"Address_en": "Kwei Tei Street, Fo Tan, Sha Tin",
"Facilities_en": "2 barbecue pits",
},
"1": {
"District_en": "Sha Tin",
"Name_en": "Lok Shun Path Barbecue Area",
"Address_en": "Lok Shun Path, Sha Tin",
"Facilities_en": "6 barbecue pits",
}
}
我试图在每个“索引”对象中专门打印出每个属性。
在HTML中使用以下代码。像循环整个对象并打印出每个属性值。
<tr *ngFor="let index of serverData | keyvalue ">
<td>{{index.value.District_en}}</td>
<td>{{index.value.Name_en}}</td>
<td>{{index.value.Address_en}}</td>
<td>{{index.value.Facilities_en}}</td>
</tr>
但是错误对象是类型“未知”
出现的。
我发现类似的问题,但大多数是关于访问多个对象的问题。
当我的大脑变成一个单个对象内的多个对象的情况时,我的大脑停止工作...
提前致谢。
In angular , I am trying to use *ngFor
in html file to access nested objects value . The object serverData
I try to access is as followed.
{//whole object i try to loop through and access
"0": {//inner , nested object that store the data I want to access
"District_en": "Sha Tin",
"Name_en": "This will be the new name",
"Address_en": "Kwei Tei Street, Fo Tan, Sha Tin",
"Facilities_en": "2 barbecue pits",
},
"1": {
"District_en": "Sha Tin",
"Name_en": "Lok Shun Path Barbecue Area",
"Address_en": "Lok Shun Path, Sha Tin",
"Facilities_en": "6 barbecue pits",
}
}
I am trying to print out each attributes specifically inside each "index" object.
With following code in html. Like loop through whole object and print out each property value.
<tr *ngFor="let index of serverData | keyvalue ">
<td>{{index.value.District_en}}</td>
<td>{{index.value.Name_en}}</td>
<td>{{index.value.Address_en}}</td>
<td>{{index.value.Facilities_en}}</td>
</tr>
Yet error Object is of type 'unknown'
comes up.
I was finding similar question, but most are about accessing multiple object.
My brain stop working when it turns into the situation of multiple object inside a single object ...
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可以像下面这样做。
app.component.ts
app.component.html
它在 UI 上正确呈现
工作演示
如果您有任何疑问,请告诉我。
注意:更新了 ts 代码
You can do like below.
app.component.ts
app.component.html
It is rendering properly on UI
Working Demo
Let me know if you have any doubt.
Note: Updated ts code