I simply need to inject HTML in a div with id
"main-wrapper"
, so in my component.ts i am using this code
import { Component, OnInit, ElementRef,Renderer2,Inject } from '@angular/core';
import { Pipe } from '@angular/core';
import { DomSanitizer, SafeHtml, SafeStyle, SafeScript, SafeUrl, SafeResourceUrl } from '@angular/platform-browser';
@Component({
selector: 'app-editsection',
templateUrl: './editsection.component.html',
styleUrls: ['./editsection.component.css']
})
export class EditsectionComponent implements OnInit {
...//some code
constructor(
@Inject(DOCUMENT) private document: any,
private route: ActivatedRoute,
private elRef: ElementRef,
private el: ElementRef,
private _sanitizer:DomSanitizer
) { }
ngOnInit() {
var strHTML = '<p>abc<p>';
this.document.getElementById("main-wrapper").innerHTML += this._sanitizer.bypassSecurityTrustHtml(strHTML);
...
}
}
When i run the code it says: SafeValue must use [property]=binding: abc
(see http://g.co/ng/security#xss)
Why i need to implement this- Because when i am injecting innerHTML, i am loosing a property contenteditable="true"
Before applying innerHTML, my code looks as shown below:
<h1 _ngcontent-c2 contenteditable="true">Hii<h2>
After applying innerHTML it becomes:
<h1 _ngcontent-c2>Hii<h2>
Please help me resolve the problem