简体   繁体   中英

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]

 {
        "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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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