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

前端 未结 6 1602
孤独总比滥情好
孤独总比滥情好 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:19

    For Angular 6.1+ , you can use default pipe keyvalue ( Do review and upvote also ) :

    • {{recipient.key}} --> {{recipient.value}}

    WORKING DEMO


    For the previous version :

    One simple solution to this is convert map to array : Array.from

    Component Side :

    map = new Map();
    
    constructor(){
        this.map.set("sss","sss");
        this.map.set("aaa","sss");
        this.map.set("sass","sss");
        this.map.set("xxx","sss");
        this.map.set("ss","sss");
        this.map.forEach((value: string, key: string) => {
            console.log(key, value);
        });
    }
    
    getKeys(map){
        return Array.from(map.keys());
    }
    

    Template Side :

    • {{recipient}}

    WORKING DEMO

提交回复
热议问题