结合的功能与在角7所喷射的html

问题描述 投票:2回答:1

我想一个函数绑定到在运行时使用的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")
  }
}

我试过,但它似乎并没有工作。我不知道如果我错过了一些东西。任何帮助表示赞赏

angular angular-directive
1个回答
-1
投票

实施safeHtml管;它不出来框:

@Pipe({
  name: 'safeHtml'
})
export class SafeHtmlPipe implements PipeTransform {
  constructor(private sanitizer: DomSanitizer) { }

  transform(value: string): any {
    return this.sanitizer.bypassSecurityTrustHtml(value);
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.