我如何在Ionic 2的控制器中运行字符串中的函数.我的代码:
export class ProjectsPage { text:string = '' ; constructor() { this.text = '<a (click)="myAlert()">Show allert</a>' ; } myAlert() { alert(1) ; } }
解决方法
您可以使用DomSanitizationService执行此操作.导入服务
import {DomSanitizationService} from '@angular/platform-browser';
现在,在类构造函数中执行此操作
constructor(sanitizer: DomSanitizationService) { this.text = '<a (click)="myAlert()">Show allert</a>' ; this.text = sanitizer.bypassSecurityTrustHtml(this.text); }
在模板中使用这样的
<div [innerHTML]="text"></div> // here the DOM will be appended
请查看此answer以跟进发布版本和导入中的更新