我必须使用模板上的[innerHTML]标签通过打字稿输入一些html。
我试图在html上添加一个click功能:
status = "<img src='assets/hello.png' (click)='hello()' />";
但点击功能会被控制台删除:
WARNING: sanitizing HTML stripped some content (see http://g.co/ng/security#xss).
我看到有几个人提到使用&#34; DomSanitizer&#34;但无法找到解决我确切问题的示例。
答案 0 :(得分:6)
如果你使用的是[innerHTML]
,Angular会删除所有内容 Angular特定内容。
如果您需要类似的东西,可以在运行时创建组件,如https://zoharch.github.io/position/index.html中所述 或者你可以使用命令式代码来添加像
这样的事件处理程序constructor(private elRef:ElementRef, private cdRef:ChangeDetectorRef) {}
someMethod() {
this.status = "<img src='assets/hello.png' (click)='hello()' />";
this.cdRef.detectChanges();
this.elRef.nativeElement.querySelector('img').addEventListener('click', this.hello.bind(this);
}