2017-06-14 85 views
3

我怎樣才能從字符串控制器離子2.運行功能我的代碼:點擊從控制器HTML離子2

export class ProjectsPage { 

    text:string = '' ; 

    constructor() { 
     this.text = '<a (click)="myAlert()">Show allert</a>' ; 
    } 

    myAlert() { 
     alert(1) ; 
    } 

} 
+0

如何在模板中顯示'this.text'? – Duannx

+0

我更新了答案。現在檢查 –

回答

2

您可以使用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跟進在發佈更新版本和導入

2

我只能猜測你問什麼,但你的錯誤很可能是造成傳遞字符串(click)函數。更改爲:

this.text = '<a (click)="'+this.myAlert()+'">Show allert</a>' ; 

這應該有所斬斷。

+0

'錯誤:無法找到名稱'myAlert''如果我設置'this.myAlert()'模擬器掛起 – wstudiokiwi

+0

'Show allert'怎麼樣? – uksz

+0

if(click)=「'+ this.myAlert()+'」'然後函數在page init上循環運行 – wstudiokiwi