How to iterate using ngFor loop Map containing key as string and values as map iteration

前端 未结 6 1608
孤独总比滥情好
孤独总比滥情好 2020-12-02 10:51

I am new to angular 5 and trying to iterate the map containing another map in typescript. How to iterate below this kind of map in angular below is code for component:

6条回答
  •  无人及你
    2020-12-02 11:27

    Edit

    For angular 6.1 and newer, use the KeyValuePipe as suggested by Londeren.

    For angular 6.0 and older

    To make things easier, you can create a pipe.

    import {Pipe, PipeTransform} from '@angular/core';
    
    @Pipe({name: 'getValues'})
    export class GetValuesPipe implements PipeTransform {
        transform(map: Map): any[] {
            let ret = [];
    
            map.forEach((val, key) => {
                ret.push({
                    key: key,
                    val: val
                });
            });
    
            return ret;
        }
    }
    
     
  • As it it pure, it will not be triggered on every change detection, but only if the reference to the map variable changes

    Stackblitz demo

提交回复
热议问题