I'm currently working on a mobile app with Ionic and Angular. The app features various Mapbox markers that open custom popups when clicked, each displaying unique content. I want the button within the popup to redirect users to a page with more information about that specific location. Here is a snippet of my code:
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 available 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
}
An ideal solution would involve using the href
attribute within the <a>
tag, but I prefer an Angular-specific routing method to direct users to individual business pages based on the ${key} value. I've experimented with different approaches like
document.getElementByClassName("some-test-class")[0].addEventListener('click',()=>{console.log("hello!"});
, following discussions on Stack Overflow, but encountered errors such as "Cannot read property 'addEventListener' of undefined." Other solutions involving ComponentFactoryResolver
seem overly complex for this task. Any suggestions for a simpler, more direct approach or insights on why previous attempts failed would be greatly appreciated.