我如何在 Ionic 2 中从 Controller 中的字符串运行函数。我的代码:
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跟进发布版本和导入的更新
关于angular - 从 Controller HTML Ionic 2 单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44540013/