我怎样才能从字符串控制器离子2.运行功能我的代码:点击从控制器HTML离子2
export class ProjectsPage {
text:string = '' ;
constructor() {
this.text = '<a (click)="myAlert()">Show allert</a>' ;
}
myAlert() {
alert(1) ;
}
}
我怎样才能从字符串控制器离子2.运行功能我的代码:点击从控制器HTML离子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跟进在发布更新版本和导入
我只能猜测你问什么,但你的错误很可能是造成传递字符串(click)
函数。更改为:
this.text = '<a (click)="'+this.myAlert()+'">Show allert</a>' ;
这应该有所斩断。
'错误:无法找到名称'myAlert''如果我设置'this.myAlert()'模拟器挂起 – wstudiokiwi
'Show allert'怎么样? – uksz
if(click)=“'+ this.myAlert()+'”'然后函数在page init上循环运行 – wstudiokiwi
如何在模板中显示'this.text'? – Duannx
我更新了答案。现在检查 –