Event Binding in Angular
Learn how Angular connects DOM events with component methods using event binding syntax.
CIIT Training Institute
Event binding makes Angular applications interactive by allowing the component to respond to actions performed by users.
What is Event Binding?
Event binding is an Angular feature used to listen for events generated by HTML elements and execute component methods.
Angular uses parentheses () around the event name to create an event binding.
Event Binding Syntax
<element (event)="method()">
Content
</element>
(event) represents the DOM event and method() represents the component method that should execute when the event occurs.
Click Event Binding
The most common event binding is the click event.
<button (click)="welcomeStudent()">
Welcome
</button>
welcomeStudent() {
console.log(
'Welcome to CIIT Training Institute'
);
}
CIIT Training Institute
Clicking the button calls welcomeStudent().
Passing Data with Event Binding
We can pass values from the template to the component method when an event occurs.
<button
(click)="selectStudent('Sam')">
Sam
</button>
<button
(click)="selectStudent('Pradnya')">
Pradnya
</button>
<button
(click)="selectStudent('Samiksha')">
Samiksha
</button>
selectStudent(name: string) {
console.log(
'Selected student: ' + name
);
}
Event Binding with $event
Angular provides the $event object when we need information about the event.
<input
type="text"
(input)="onInput($event)"
placeholder="Enter student name">
onInput(event: Event) {
const name =
(event.target as HTMLInputElement).value;
console.log(name);
}
Multiple Event Bindings
Multiple events can be handled on the same element.
<input
(focus)="onFocus()"
(input)="onInput($event)"
(blur)="onBlur()">
Focus
Executes when the input receives focus.
(focus)
Input
Executes when the user changes the input value.
(input)
Blur
Executes when the input loses focus.
(blur)
Click
Executes when the element is clicked.
(click)
Common Event Binding Syntax
| Event | Angular Syntax | Purpose |
|---|---|---|
| Click | (click) |
Handle button or element clicks |
| Double Click | (dblclick) |
Handle double-click actions |
| Input | (input) |
Capture input changes |
| Change | (change) |
Handle value changes |
| Key Down | (keydown) |
Handle key press |
| Key Up | (keyup) |
Handle key release |
| Focus | (focus) |
Handle focus event |
| Blur | (blur) |
Handle focus loss |
| Submit | (submit) |
Handle form submission |
Complete Example – CIIT Training Institute
selectedStudent = '';
selectStudent(name: string) {
this.selectedStudent = name;
}
welcomeStudent() {
console.log(
'Welcome to CIIT Training Institute'
);
}
<h2>
CIIT Training Institute
</h2>
<p>
Select your name:
</p>
<button
class="btn btn-primary me-2 mb-2"
(click)="selectStudent('Sam')">
Sam
</button>
<button
class="btn btn-primary me-2 mb-2"
(click)="selectStudent('Pradnya')">
Pradnya
</button>
<button
class="btn btn-primary mb-2"
(click)="selectStudent('Samiksha')">
Samiksha
</button>
<p class="mt-3">
Selected Student:
{{ selectedStudent }}
</p>
<button
class="btn btn-success"
(click)="welcomeStudent()">
Welcome to CIIT
</button>
CIIT Training Institute
Select your name:
Selected Student: Sam
Event Binding Flow
Event Binding Best Practices
- Use parentheses () for event binding.
- Keep event handler methods short and meaningful.
- Use $event when event information is required.
- Pass only the required values to component methods.
- Avoid complex business logic inside the HTML template.
- Use meaningful method names such as selectStudent().
- Keep interactive components reusable and maintainable.
Summary
Angular Event Binding connects DOM events with component methods.
The basic syntax is: (event)="method()"
Event binding can handle click, input, keyboard, mouse and form events. The $event object can be used when information about the event is required.
Event binding is an important part of creating interactive applications at CIIT Training Institute.