I'm building a content system where I want users to be able to create a post with a block of text. If they use the '#' symbol, I add it to my database. Then in my front end, I show the text when a visitor comes.
I want to be able to parse out all of the #'s and pass them through the routeLink to search for all posts that have a matching #.
This is all working with one exception. When I pass in my text to the user with:
LinkAllHashes(t: string){
this.bodyText = t.replace(/(^|\s)(#[a-zA-Z\d][\w-]*)/ig, "$1<span (click)='SearchAllHashes($2)' class='hash_tag'>$2</span>");
Everything works but it ignores the (click) event. I've been reading all over the place and it appears that I can't attach a function inside of an innerHtml at runtime. Is this so? How can I get around this?
If I simply do this:
PipeAllHashes(t: string){
this.comcom = t.replace(/(^|\s)(#[a-zA-Z\d][\w-]*)/ig, "$1<a href='search-results/$2)' class='hash_tag'>$2</a>");
It works but then it reloads the page and is terrible for user experience. This is why I need to use routeLink.
Any ideas?
*UPDATE
So after much banging my head against the wall I've decided that my current method is best case (pending a better solution).
- get the string.
- replace the string #*'s with a
- use innerHtml
I'm basing this on this screenshot from a random tweet I inspected.
Any better ideas and I'm all ears.