javascript - Render object of objects in Angular - Stack Overflow

How I could render an object of objects in Angular but without turn it in an array or anything similar.

How I could render an object of objects in Angular but without turn it in an array or anything similar. The reason for this is that I have a large object of objects and I would like to avoid to many iterations from object to an array after that to loop the array in the html.

For example I have:

    persons: any = {
      0: { name: 'name1', email: '[email protected]' },
      1: { name: 'name2', email: '[email protected]' },
      2: { name: 'name3', email: '[email protected]' }
    }

When I do is the following:

    <div *ngFor="let person of persons | keyvalue">
      <p>
      {{ person.key }} / {{ person.value }}
      </p>
    </div>

The result is:

    0 / [object Object]
    1 / [object Object]
    2 / [object Object]

I tried to loop one more time thru the person.value:

    <div *ngFor="let person of persons | keyvalue;">
      <p>
      {{ person.key }}
      <span *ngFor="let item of person.value | keyvalue"></span>
      </p>
    </div>

but this causing an error ("No overload matches this call.").

Could anyone tell me how could I achieve this?

How I could render an object of objects in Angular but without turn it in an array or anything similar. The reason for this is that I have a large object of objects and I would like to avoid to many iterations from object to an array after that to loop the array in the html.

For example I have:

    persons: any = {
      0: { name: 'name1', email: '[email protected]' },
      1: { name: 'name2', email: '[email protected]' },
      2: { name: 'name3', email: '[email protected]' }
    }

When I do is the following:

    <div *ngFor="let person of persons | keyvalue">
      <p>
      {{ person.key }} / {{ person.value }}
      </p>
    </div>

The result is:

    0 / [object Object]
    1 / [object Object]
    2 / [object Object]

I tried to loop one more time thru the person.value:

    <div *ngFor="let person of persons | keyvalue;">
      <p>
      {{ person.key }}
      <span *ngFor="let item of person.value | keyvalue"></span>
      </p>
    </div>

but this causing an error ("No overload matches this call.").

Could anyone tell me how could I achieve this?

Share Improve this question asked Sep 21, 2021 at 14:48 Atanas IvanovAtanas Ivanov 691 silver badge8 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 5

You can just use the json pipe

example:

{{persons | json}}

Documentation: https://angular.io/api/mon/JsonPipe

Stackblitz example

The keyvalue pipe transforms 0: { name: 'name1', email: '[email protected]' } into { key : 0, value : { name: 'name1', email: '[email protected]' }}. So you should be able to do :

<div *ngFor="let person of persons | keyvalue">
   <p>
      {{ person.value.name}} / {{ person.value.email }}
   </p>
</div>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745048034a4608215.html

相关推荐

  • javascript - Render object of objects in Angular - Stack Overflow

    How I could render an object of objects in Angular but without turn it in an array or anything similar.

    7小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信