angular – 从控制器HTML Ionic 2中单击

前端之家收集整理的这篇文章主要介绍了angular – 从控制器HTML Ionic 2中单击前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我如何在Ionic 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以跟进发布版本和导入中的更新

猜你在找的Angularjs相关文章