How to iterate object keys using *ngFor?

后端 未结 5 617
温柔的废话
温柔的废话 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:13

    Update

    In 6.1.0-beta.1 KeyValuePipe was introduced https://github.com/angular/angular/pull/24319

    {{ item.key }} - {{ item.value }}

    Plunker Example

    Previous version

    You could try something like this

    export class ObjNgFor implements PipeTransform {
        transform(value: any, args: any[] = null): any {
            return Object.keys(value).map(key => Object.assign({ key }, value[key]));
        }
    }
    

    And then on your template

      
    {{obj.key}} - {{obj.description}}

    Plunker

提交回复
热议问题