Reputation: 900
How i can run function from string in controller in Ionic 2. My code:
export class ProjectsPage {
text:string = '' ;
constructor() {
this.text = '<a (click)="myAlert()">Show allert</a>' ;
}
myAlert() {
alert(1) ;
}
}
Upvotes: 4
Views: 256
Reputation: 8484
You can do this by using DomSanitizationService
. Import the service
import {DomSanitizationService} from '@angular/platform-browser';
Now, in class constructor do this
constructor(sanitizer: DomSanitizationService) {
this.text = '<a (click)="myAlert()">Show allert</a>' ;
this.text = sanitizer.bypassSecurityTrustHtml(this.text);
}
Use like this in template
<div [innerHTML]="text"></div> // here the DOM will be appended
Have a look at this answer to follow up the updates in the release versions and imports
Upvotes: 3
Reputation: 18717
I can only guess what you are asking for, but your mistake is most likely caused by passing string in (click)
function. Change is to:
this.text = '<a (click)="'+this.myAlert()+'">Show allert</a>' ;
This should do the trick.
Upvotes: 2