【发布时间】:2019-11-02 08:25:46
【问题描述】:
我正在尝试使用 angular 在同一页面上呈现多个传单地图。 ngFor 用于动态渲染地图。地图的数量取决于从 REST 服务接收的数据。
map.component.html
<div *ngFor="let m of mapIds">
<div class="map" [id]="m"></div>
</div>
map.component.ts
[版本 1] - this.mapIds 中未填充数据
ngOnInit() {
PopulateData();
// do other stuff
}
PopulateData() {
// Invoke Web service
this.cService.getMapData(cInput).subscribe(
(res: getMapData[]) =>
{
// form a list of mapIds
this.mapIds = mapIds
}
}
ngAfterViewInit() {
// Initialize leaflet maps
this.mapIds.forEach( x => { // <--- this.mapIds is null
this.maps[x] = L.map(x, {
attributionControl: false,
minZoom: 3,
maxZoom: 7,
zoomControl: false
});
// rest of map initialization
})
}
[版本 2] 给出以下错误
错误:找不到地图容器。
ngOnInit() {
PopulateData();
// do other stuff
InitMaps();
}
PopulateData() {
// Invoke Web service
this.cService.getMapData(cInput).subscribe(
(res: getMapData[]) =>
{
// form a list of mapIds
this.mapIds = mapIds
}
}
InitMaps() {
// Initialize leaflet maps
this.mapIds.forEach( x => {
this.maps[x] = L.map(x, { // <-- Error thrown here
attributionControl: false,
minZoom: 3,
maxZoom: 7,
zoomControl: false
});
// rest of map initialization
})
}
有没有办法等到this.mapIds 填充到版本 1 中?或者有什么其他建议?
【问题讨论】:
-
代码中的“L”是什么?