angular - 从 Controller HTML Ionic 2 单击

标签 angular ionic-framework ionic2

我如何在 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/

相关文章:

javascript - 如何在模式关闭时同步 firebase 数据而不用 ionic 重新加载页面?

authentication - Ionic Framework 和 Firebase 持久身份验证

angularjs - 如何在Ionic 3中拦截动态链接

即使安装了定义文件, typescript 也找不到模块

angular - Angular 2 @Component 语法是 ES6 的一部分吗?

Angular 12 child 访问模板占位符

java - 将日期从 Angular 日期选择器传递到java后端的理想方法?

typescript - Ionic 2 - 在页面之间导航的侧面菜单(后退按钮)

angularjs - 单击时将 id 值传递给 ionic 模式

javascript - 在 Typescript 中使用 CellRenderer 时出现 Ag-grid 错误