在谷歌地图信息窗口angular2中显示自定义标签

Raj*_*bel 4 google-maps angular

map.component.ts 代码:

......
infoWindow = new google.maps.InfoWindow(
{
    content: `<custom-tag></custom-tag>`    //***not displaying anything***
});
infoWindow.open(map, marker);
......
Run Code Online (Sandbox Code Playgroud)

map.component.html 代码:

<custom-tag></custom-tag>      <!--***displays hi***-->
<div class="google-maps"></div>
Run Code Online (Sandbox Code Playgroud)

custom-tag.component.html 代码:

<h2>hi</h2>
Run Code Online (Sandbox Code Playgroud)

module.ts、routing.ts 文件肯定没有错误。信息窗口只是打开并没有显示任何内容,请帮助我弄清楚为什么信息窗口没有显示任何内容。

yur*_*zui 8

您必须通过动态创建组件 ComponentFactoryResolver

const compFactory = this.resolver.resolveComponentFactory(CustomTag);
this.compRef = compFactory.create(this.injector);

this.appRef.attachView(this.compRef.hostView);

let div = document.createElement('div');
div.appendChild(this.compRef.location.nativeElement);

this.infoWindow.setContent(div);
this.infoWindow.open(this.map, marker);
Run Code Online (Sandbox Code Playgroud)

这是Plunker示例

不要忘记添加CustomTag组件entryComponents