Event Binding
Learn how Angular uses event binding to respond to user actions and send event information from the template to the component.
What is Event Binding?
Event binding is an Angular data binding technique used to listen for user actions such as clicks, typing, mouse events, and keyboard events.
It uses parentheses (event) around the event name
to connect an HTML event with a component method.
Event Binding Flow
User Action
The user performs an action on the UI.
Event
Browser generates an event.
Binding
Angular connects the event to a handler.
Component
The component method handles the event.
Main Event Binding Concepts
Click Event
Respond to button and element click actions.
Input Event
Respond when the value of an input changes.
Keyboard Event
Handle keyboard actions such as key presses.
Mouse Event
Respond to mouse interactions with elements.
Event Binding Syntax
Event binding uses parentheses around the event name.
<element (event)="handler()">
</element>
When the specified event occurs, Angular executes the expression or method written inside the event binding.
Click Event
The click event is commonly used to respond to
button clicks.
export class StudentComponent {
showMessage() {
console.log('Button clicked');
}
}
<button (click)="showMessage()">
Show Message
</button>
Event Binding with an Expression
A simple statement can also be written directly inside the event binding.
<button
(click)="message = 'Hello Sam!'">
Click Me
</button>
When the button is clicked, Angular updates the component
property message.
Input Event
The input event occurs whenever the value entered
in an input control changes.
<input
(input)="onInput($event)"
placeholder="Enter student name">
onInput(event: Event) {
console.log(
(event.target as HTMLInputElement).value
);
}
Event Object
Angular provides the browser event object through the special
$event variable.
<button (click)="handleClick($event)">
Click Me
</button>
handleClick(event: MouseEvent) {
console.log(event);
}
The $event object contains information about
the event that occurred.
Keyboard Events
Angular can listen to keyboard events such as
keyup and keydown.
<input
(keyup)="onKeyUp($event)"
placeholder="Type something">
onKeyUp(event: KeyboardEvent) {
console.log(event.key);
}
Mouse Events
Mouse events can be used to respond to user interactions such as entering or leaving an element.
<div
(mouseenter)="showMessage()"
(mouseleave)="hideMessage()">
Student Profile
</div>
Common mouse events include
click, mouseenter,
mouseleave, mousedown,
and mouseup.
Passing Parameters to Methods
Event binding can call a method and pass additional values.
<button
(click)="selectStudent('Sam')">
Select Student
</button>
selectStudent(name: string) {
console.log(
'Selected: ' + name
);
}
Selected: Sam
Event Data Flow
User
Click Button
Browser
click event
Angular
(click)
Component
showMessage()
Common Angular Events
| Event | Syntax | Common Usage |
|---|---|---|
| Click | (click) |
Button and element clicks |
| Input | (input) |
Input value changes |
| Change | (change) |
Control value changes |
| Keyup | (keyup) |
Keyboard release |
| Keydown | (keydown) |
Keyboard press |
| Mouse Enter | (mouseenter) |
Mouse enters element |
| Mouse Leave | (mouseleave) |
Mouse leaves element |
Property Binding vs Event Binding
| Feature | Property Binding | Event Binding |
|---|---|---|
| Syntax | [property] |
(event) |
| Direction | Component → Template | Template → Component |
| Purpose | Set element properties | Handle user events |
| Example | [disabled]="isDisabled" |
(click)="showMessage()" |
| Common Usage | Dynamic element state | User interaction |
Common Uses of Event Binding
Button Actions
Execute component methods when buttons are clicked.
Form Input
Respond to changes in input controls.
Keyboard Actions
Handle keyboard events from users.
Mouse Actions
Respond to mouse interactions.
Complete Event Binding Example
export class StudentComponent {
studentName = 'Sam';
message = '';
showStudent() {
this.message =
'Student: ' + this.studentName;
}
}
<h2>
{{ studentName }}
</h2>
<button
(click)="showStudent()">
Show Student
</button>
<p>
{{ message }}
</p>
Student Details
Name: Sam
Student: Sam
Best Practices
Use clear method names such as
showStudent() or submitForm().
Avoid putting large amounts of business logic directly inside template event expressions.
Pass $event when the component needs details
about the browser event.
Add event bindings only when the UI needs to respond to that particular user action.
Move reusable application logic into component methods or services instead of duplicating it in templates.
Summary
- Event binding connects user actions with component logic.
- It uses parentheses such as
(click). - Angular supports mouse, keyboard, input, and other browser events.
$eventprovides access to the event object.- Event binding generally moves information from the template to the component.
- Event binding makes Angular applications interactive.