1

I try to detect an Url or plain text in a string value. If it's an Url, the function return a HTML hyperlink using bypassSecurityTrustHtml. The hyperlink is displayed, but when I click on it, nothing happen. However, Chrome recognize it as a link and when I right click, I can select «Open in a new window» and it works.

Angular code:

public formatReservedField(value: string) {
  let pattern = /(http|https|ftp|ftps)\:\/\//gm;

  if (pattern.test(value)) {
    return this._sanitizer.bypassSecurityTrustHtml("<a href='" + value + "' target='_blank'>" + value + "</a>");
  }

  return value;
}

HTML code:

<div [innerHTML]="formatReservedField(reservedField)"></div>

1 Answer 1

4

Ok, I've found it!

I've set the change detection of my component to OnPush strategy and it works now:

@Component( {
    ...
    changeDetection: ChangeDetectionStrategy.OnPush,
    ...
}
Sign up to request clarification or add additional context in comments.

2 Comments

What on earth, why does this work? After using pipes all day to fix my user content... This is the key to the entire clickable hyperlink issue. <div [innerHTML]="Value | safeHtml"></div>
why??????? who can tell me why...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.