7

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?

1 Answer 1

21

You can use square bracket syntax like

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

where

action="clickMe"
Sign up to request clarification or add additional context in comments.

2 Comments

just mention: the action should be the string of the method name
This gives me an error like this error TS7053: Element implicitly has an 'any' type because expression of type 'any' can't be used to index type 'Component'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.