Binding (click) in [innerhtml] with angular (ionic) for mention @ or Hashtags #

Viewed 309

Hi try to dinamically add link to some part of a text submit by a user like :

  submitText = '#test @mention http://google.com'

Rendering html :

[innerHTML]="submitText | dynamiccontent"

But result after rendering is : #test @mention http://google.com but the (click)="goToPost($2)" is print and not processed by angular.

<span class="font-bold" (click)="goToPost(test)">#test</span>

Objective is : if # found (click)="goToPost($2)" or if @ found (click)="goToProfile($2)"

Here is the pipe dynamiccontent.ts

import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer, SafeHtml, SafeStyle, SafeScript, SafeUrl, SafeResourceUrl} from '@angular/platform-browser';

@Pipe({ name: 'dynamiccontent' })

export class DynamicContentPipe implements PipeTransform {
    constructor(private sanitizer: DomSanitizer) {}

    urls: any = /(\b(https?|http|ftp|ftps|Https|rtsp|Rtsp):\/\/[A-Z0-9+&@#\/%?=~_|!:,.;-]*[-A-Z0-9+&@#\/%=~_|])/gim; // Find/Replace URL's in text  
    hashtags: any = /(^|\s)(#[a-z\d][\w-]*)/ig; // Find/Replace #hashtags in text   
    mentions: any = /(^|\s)(@[a-z\d][\w-]*)/ig; // Find/Replace @Handle/Mentions in text    
    emails: any = /(\S+@\S+\.\S+)/gim; // Find/Replace email addresses in text

    transform(text) {
        return this.sanitizer.bypassSecurityTrustHtml(this.parseUrl(text));
    }

    private parseUrl(text: string) {
        // Find/Replace URL's in text
        if (text.match(this.urls)) {
                text = text.replace(this.urls, function replacer($1, $2, $3) {
                        let url: any = $1;
                        let urlClean: any = url.replace("" + $3 + "://", "");

                        return "<a class=\"font-bold noline\" href=\"" + url + "\" target=\"_blank\">" + url + "</a>";
                });
        }

        // Find/Replace @Handle/Mentions in text
        if (text.match(this.hashtags)) {
            text = text.replace(this.hashtags, "<span class=\"font-bold \" (click)=\"goToPost($2)\">$1$2</span>");
        }

        // Find/Replace #hashtags in text
        if (text.match(this.mentions)) {
            text = text.replace(this.mentions, "<a class=\"font-bold noline\" href=\"/search/handle/$2\" >$1$2</a>");
        }

        // Find/Replace email addresses in text
        //if (text.match(this.emails)) {
        //        text = text.replace(this.emails, "<a href=\"mailto:$1\">$1</a>");
        //}


        return text;
    }  
}
0 Answers
Related