Recently, I encountered a requirement that involved converting a part of plain text into a clickable link and then adding a click handler to it. The goal was to display some details in a popup when the link is clicked.
In order to convert the normal string into a link, I utilized the code below. However, I faced issues with attaching a click event listener to it:
const arrayLabel = text.split('$');
let stylizedText = arrayLabel[0];
stylizedText += `<a id="anchorId" style="text-decoration: none;" href="javascript:void(0)">${arrayLabel[1]}</a> `;
stylizedText += arrayLabel[2];
return stylizedText;
I attempted approaches like (click)="myMethod"
and
document.getElementById('anchorId").addEventListener('click','myMethod')
, but unfortunately, both methods were unsuccessful. Can someone offer assistance with this problem?