I know this same question has been asked many times before, but no solution worked for me. And I already have spent many hours on it.
In my case I am simply replacing a text with with an html element and on onclick event I want to call an event or a function defined in the same class.
var linkToReplace = '<a onclick="this.events.publish(\'issue:display\')">'+issueKey+'</a>';
Error is given below:
Uncaught TypeError: Cannot read property 'publish' of undefined.
it works for "alert(\'hello\')", but all other functions got undefined.
I strongly suggest not mixing templates and component code. Even if you get it to work in development mode, it will break in mysterious ways with the ahead-of-time compiler. Keep template code in templates.
I understand but I am in a situation that I need to replace sequence of characters if found in text with clickable links. I am still not able find a better solution for this.
There is always another way. You could parse these “sequences of characters” into a heterogeneous array of objects, loop across this array with an ngFor and render links when the object is a link type.
I guess other peoples’ experiences may vary, but every single time I have started out doing anything with HTML inside an Angular 2 component, I eventually gave up and did literally anything to avoid doing so. The framework really really does not want you to be doing this.
I just had to take a moment from banging my head against various solid objects to second this statement. It is so obtusely difficult to work with anything beyond basic html content outside of a template in angular2. Very frustrating if you’re trying to keep your data uncoupled from the view components.