我想一个函数绑定到在运行时使用的innerHTML注入的HTML。我的组件
@Component({
selector: 'my-app',
template: `<div [innerHtml]="myHTML | safeHtml"></div>`,
styleUrls: ['/my-app.css'], encapsulation: ViewEncapsulation.ShadowDom
})
export class MyApp implements OnInit {
myHTML = `<button (click)="clickMe()" type="button" class="btn btn-secondary">+</button>`
constructor() {}
clickMe() {
console.log("Function is binded using the inner html tag")
}
}
我试过,但它似乎并没有工作。我不知道如果我错过了一些东西。任何帮助表示赞赏
实施safeHtml
管;它不出来框:
@Pipe({
name: 'safeHtml'
})
export class SafeHtmlPipe implements PipeTransform {
constructor(private sanitizer: DomSanitizer) { }
transform(value: string): any {
return this.sanitizer.bypassSecurityTrustHtml(value);
}
}