我只需要在 div 中注入 HTML id
"main-wrapper"
,所以在我的 component.ts 中我正在使用这段代码
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);
...
}
}
当我运行代码时,它说: SafeValue must use [property]=binding: abc
(见http://g.co/ng/security#xss)
为什么我需要实现这个 - 因为当我注入 innerHTML 时,我失去了一个属性contenteditable="true"
在应用 innerHTML 之前,我的代码如下所示:
<h1 _ngcontent-c2 contenteditable="true">Hii<h2>
应用 innerHTML 后,它变为:
<h1 _ngcontent-c2>Hii<h2>
请帮我解决问题