0

The (click) event is not working in my component.

HTML:

<button (click)="onClick">Click</button>

Below is the method in my component class.

onClick(): void { console.log('Button Clicked'); }

2 Answers 2

4

onClick should be a function

Try this

<button (click)="onClick()">Click</button>

Here is the working example

https://stackblitz.com/edit/angular-oudvby?file=app/app.component.ts

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the answer, I have added the function body in the question. That is defined in my component class.
@Jith try that link
any thing I need to import in my component class to make it work?
no you don't just check the example I have provided. you just need to replace onClick with onClick().
Which isn't a function :)
3

try this

<button (click)="onClick()">Click</button>

or

<button on-click="onClick()">Click</button>

working example

1 Comment

Thanks for the answer. I have same button declaration in my template file.

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.