我是 angular 5 的新手,并试图在打字稿中迭代包含另一个地图的地图。如何在下面的角度迭代这种地图是组件的代码:
import { Component, OnInit} from '@angular/core';
@Component({
selector: 'app-map',
templateUrl: './map.component.html',
styleUrls: ['./map.component.css']
})
export class MapComponent implements OnInit {
map = new Map<String, Map<String,String>>();
map1 = new Map<String, String>();
constructor() {
}
ngOnInit() {
this.map1.set("sss","sss");
this.map1.set("aaa","sss");
this.map1.set("sass","sss");
this.map1.set("xxx","sss");
this.map1.set("ss","sss");
this.map1.forEach((value: string, key: string) => {
console.log(key, value);
});
this.map.set("yoyoy",this.map1);
}
}
它的模板html是:
<ul>
<li *ngFor="let recipient of map.keys()">
{{recipient}}
</li>
</ul>
<div>{{map.size}}</div>
原文由 Feroz Siddiqui 发布,翻译遵循 CC BY-SA 4.0 许可协议
对于 Angular 6.1+ ,您可以使用默认管道
keyvalue
( 也进行审查和 投票):工作演示
对于以前的版本:
一个简单的解决方案是将映射转换为数组: Array.from
组件侧:
模板面:
工作演示