简体   繁体   English

在 Angular 10 中使用 *ngFor 迭代对象键

[英]iterate object keys using *ngFor in Angular 10

I want to iterate the "others" with *ngFor I got the "id" and "year" correct but when I try to iterate "others", I got [object object]我想用 *ng 迭代“其他人”,因为我得到了正确的“id”和“年份”,但是当我尝试迭代“其他人”时,我得到了 [object object]

 {
        "id": 11,
        "year": [
        2019,
        2020
        ],
        "others": {
        "name": "John",
        "age": "19",
        "work": "yes",
        },
    }

I got it, By using the safe navigation ?我明白了,通过使用安全导航? So it will be data.others?.name所以它将是data.others?.name

You can't do string interpolation to call an object, but you should call any single property from the object.您不能通过字符串插值来调用对象,但您应该调用对象中的任何单个属性。

<h1>{{others}}</h1> // output [object object]


// do like this

<h1>{{others.name}}</h1>

And object is not a something that you can iterate对象不是可以迭代的东西

You'll have to add the “key value” pipe for you to be able to use *ngFor with objects.您必须添加“键值”管道才能将 *ngFor 与对象一起使用。

<div *ngFor="let item of object | keyvalue">
    Key: <b>{{item.key}}</b> and Value: <b>{{item.value}}</b>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM