1

I have met an unexpected behavior for me of contenteditable attribute in Angular. I have an object with HTML, stored as a value:

public json_html = {
  "button1":"<p contenteditable='true'>first section</p>",
  "button2":"<p>second section</p>",
  "button3":"<p>third section</p>",
}

And I apply this values like this (via innerHTML):

<div [innerHTML]="selectedButton"></div>

Everything works fine except contenteditable attribute - it's just missed in HTML:

enter image description here

QUESTION:

How to force contenteditable attribute to work (when element becomes through [innerHTML])? Is there a proper way to do that or may be there is a workaround?

LIVE EXAMPLE: https://stackblitz.com/edit/angular-9pyhg3-lnivvj?file=app%2Fbutton-overview-example.html

P.S.
  • 15,970
  • 14
  • 62
  • 86

1 Answers1

1

That attribute is stripped for security reasons

If you tell Angular that it should treat it as safe, use DomSanitizer

  constructor(sanitizer: DomSanitizer) {
    this.json_html =  {
    "button1": sanitizer.bypassSecurityTrustHtml("<p contenteditable='true'>first section</p>"),
    "button2":"<p>second section</p>",
    "button3":"<p>third section</p>",
  }

StackBlitz example

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567