【发布时间】:2021-07-05 16:13:43
【问题描述】:
我正在使用 Ionic 和 Angular 开发一个移动应用程序。我在地图上呈现了几个 Mapbox 标记,每个标记都在单击时显示带有自定义内容的弹出窗口。我希望弹出窗口中的按钮将用户重定向到包含有关该特定位置的更多详细信息的页面。我的代码如下:
ionViewWillEnter(){
this.businessService
.getBusinessCoords()
.subscribe(data=>{
for(const key in data){
let popup = new mapboxgl.Popup({
className:'popup-class'
}).setHTML(
`<div>
<img src= ${data[key].profile_picture} width="150">
<b style="text-align: center;"> ${data[key].name}</b><br>
<i>${data[key].location.address}</i><br>
<a id="${key}">Tap to see availble offers!</a>
</div>
`
)
new mapboxgl.Marker({
color:"#cc0000",
draggable: false
}).setLngLat([data[key].location.coord.lng, data[key].location.coord.lat])
.setPopup(popup)
.addTo(this.map);
}
})//end subscribe
}
当然,即用型方法将使用<a>标签的href属性,但我想使用一些特定于Angular的路由方法来路由到显示的每个业务的相应页面在地图上(路线应该取决于 ${key})。到目前为止,我已经尝试在将some-test-class 类添加到<a> 标记后使用document.getElementByClassName("some-test-class")[0].addEventListener('click',()=>{console.log("hello!"});,并按照建议here 和in the 3rd answer here 与document.getElementById() 类似的东西@ 并且我得到“无法读取未定义的属性'addEventListener' ”。我还尝试了here 列出的第二种方法,但这似乎也不起作用。我还看到了更复杂的解决方案,涉及ComponentFactoryResolver(this question 的公认答案),但在试图解决似乎微不足道的问题之前,我想寻求一个更简单、更-直接方法。或者也许是我之前尝试失败的原因的建议。
【问题讨论】:
标签: angular typescript ionic-framework mapbox-gl-js ionic5