Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic function name in Angular 2 click event

While this answer is probably dead simple, I seem to be stuck. As an Angular 2 beginner, I have tried all possible combinations of {}, [] and () brackets to achieve the following:

<button (click)="this.action">Click me</button>

where:

this.action = "clickMe()"

i.e. it is a string, a name of the method in that component that should be executed on click.

Any ideas?

like image 761
dvdbrk Avatar asked Dec 17 '16 10:12

dvdbrk


1 Answers

You can use square bracket syntax like

(click)="this[action]()"

where

action="clickMe"
like image 70
yurzui Avatar answered Sep 19 '22 12:09

yurzui