Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 从控制器HTML 2中单击_Angular_Ionic Framework_Ionic2 - Fatal编程技术网

Angular 从控制器HTML 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='';

如何在离子2的控制器中从字符串运行函数。我的代码:

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