I just need to inject HTML into the div using id "main-wrapper", so in my component.ts component i use 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 {
...
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 should use [property] = binding: ABC
(see http://g.co/ng/security#xss )
Why do I need to implement this? Because when I insert innerHTML, I lose the propertycontenteditable="true"
Before applying innerHTML, my code looks like this:
<h1 _ngcontent-c2 contenteditable="true">Hii<h2>
After applying innerHTML it becomes:
<h1 _ngcontent-c2>Hii<h2>
Please help me solve the problem.