Event Binding in Angular

In this guide, we will explore the Event Binding in Angular. Event binding is one way from view to component. We use it to perform an action in the component when the user performs an action like clicking on a button, changing the input, etc in the view.

Applies to: Angular 2 to the latest edition of i.e. Angular 8. Angular 9, Angular 10

Event Binding

Event binding allows us to bind events such as keystroke, clicks, hover, touche, etc to a method in component. It is one way from view to component. By tracking the user events in the view and responding to it, we can keep our component in sync with the view. For Example, when the user changes to an input in a text box, we can update the model in the component, run some validations, etc. When the user submits the button, we can then save the model to the backend server.

Syntax

The Angular event binding consists of two parts

  • We enclose the target event name in parentheses on the left side
  • Assign it to a template statement within a quote on the right side

Angular event binding syntax consists of a target event name within parentheses on the left of an equal sign, and a quoted template statement on the right.

The following event binding listens for the button’s click events, calling the component’s onSave() method whenever a click occurs

Event Binding in Angular

Event Binding Example

Create a new angular application

Copy the following code to app.component.html

Add the following code to the app.component.ts

In the above example, the component listens to the click event on the button. It then executes the clickMe() method and increases the clickCount by one.

Template statements have side effects

Unlike the Property Binding & Interpolation, where we use the template expression is used, in case of event binding we use template statement.

The Template statement can change the state of the component. Angular runs the change detection and updates the view so as to keep it in sync with the component.

on-

Instead of parentheses, you can also use the on- syntax as shown below.

Multiple event handlers

You can also bind an unlimited number of event handlers on the same event by separating them with a semicolon :

Add a new component property

And in the template, call clickMe() method and then an assignment clickCount1=clickCount

$event Payload

DOM Events carries the event payload. I.e the information about the event. We can access the event payload by using $event as an argument to the handler function.

And in the component

The properties of a $event object vary depending on the type of DOM event. For example, a mouse event includes different information than an input box editing event.

Remember you need to use the variable as $event in the Template statement. Example handleInput($event). Otherwise, it will result in an error

Template reference variable

We can also make use of the template reference variable to pass the value instead of $event.

In the template

Key event filtering (with key.enter)

We use keyup/keydown events to listen for keystrokes. The following example does that

But Angular also offers a feature, where it helps to filter out certain keys. For Example, if you want to listen only to the enter keys you can do it easily

Here is an interesting example. On pressing enter key it updates the value3 variable and on escape clears the variable.

Angular calls these pseudo-events.

You can also listen for the key combination

Custom events with EventEmitter

Directives & components can also raise events with EventEmitter. Using EventEmiiiter you can create a property and raise it using the EventEmitter.emit(payload). The Parent component can listen to these events using the event binding and also read the payload using the $event argument.

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top