Angular 从控制器HTML 2中单击
如何在离子2的控制器中从字符串运行函数。我的代码:Angular 从控制器HTML 2中单击,angular,ionic-framework,ionic2,Angular,Ionic Framework,Ionic2,如何在离子2的控制器中从字符串运行函数。我的代码: export class ProjectsPage { text:string = '' ; constructor() { this.text = '<a (click)="myAlert()">Show allert</a>' ; } myAlert() { alert(1) ; } } 导出类项目页面{ text:string='';
export class ProjectsPage {
text:string = '' ;
constructor() {
this.text = '<a (click)="myAlert()">Show allert</a>' ;
}
myAlert() {
alert(1) ;
}
}
导出类项目页面{
text:string='';
构造函数(){
this.text='Show allert';
}
myAlert(){
警报(1);
}
}
我只能猜测您的要求,但您的错误很可能是由于在(单击)
函数中传递字符串造成的。更改为:
this.text = '<a (click)="'+this.myAlert()+'">Show allert</a>' ;
this.text='Show allert';
这应该可以做到。您可以通过使用
DomainSanitationService
来做到这一点。导入服务
import {DomSanitizationService} from '@angular/platform-browser';
现在,在类构造函数中执行此操作
constructor(sanitizer: DomSanitizationService) {
this.text = '<a (click)="myAlert()">Show allert</a>' ;
this.text = sanitizer.bypassSecurityTrustHtml(this.text);
}
构造函数(消毒器:DomSanitationService){
this.text='跟踪发布版本中的更新并导入错误:如果我设置了this.myAlert()
emulator,则找不到名称“myAlert”
,关于“Show allert”?如果(单击)=“+this.myAlert()+”“
然后函数在第一页循环运行如何在模板中显示此文本?我更新了答案。现在检查它
<div [innerHTML]="text"></div> // here the DOM will be appended