adev/src/content/tutorials/learn-angular/steps/7-event-handling/README.md
Event handling enables interactive features on web apps. It gives you the ability as a developer to respond to user actions like button presses, form submissions and more.
NOTE: Learn more about handling user interaction in the essentials guide.
In this activity, you'll learn how to add an event handler.
<hr />In Angular you bind to events with the parentheses syntax (). On a given element, wrap the event you want to bind to with parentheses and set an event handler. Consider this button example:
@Component({
...
template: `<button (click)="greet()">`
})
export class App {
greet() {
console.log('Hello, there 👋');
}
}
In this example, the greet() function will run every time the button is clicked. Take note that the greet() syntax includes the trailing parenthesis.
Alright, your turn to give this a try:
<docs-workflow> <docs-step title="Add an event handler"> Add the `showSecretMessage()` event handler function in the `App` class. Use the following code as the implementation:showSecretMessage() {
this.message = 'Way to go 🚀';
}
<section (mouseover)="showSecretMessage()">
And with a few steps in the code you've created your first event handler in Angular. Seems like you are getting pretty good at this, keep up the good work.