How to iterate object keys using *ngFor?

后端 未结 5 614
温柔的废话
温柔的废话 2020-11-27 20:46

I\'ve been digging around, and found out that I can use the following to use *ngFor over an object:

 
...
5条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-27 21:05

    Or instead of creating a pipe and passing an object to *ngFor, just pass Object.keys(MyObject) to *ngFor. It returns the same as the pipe, but without the hassle.

    On TypeScript file:

    let list = Object.keys(MyObject); // good old javascript on the rescue
    

    On template (html):

    *ngFor="let item of list"
    

提交回复
热议问题