Event Handling Introduction
Learn how Angular handles user interactions such as clicks, keyboard actions, input changes, mouse events and form submissions.
What is Event Handling?
Event handling in Angular is the process of responding to actions performed by the user on a web page.
For example, when Sam clicks a button, Angular can execute a component method. Similarly, when Pradnya types inside an input box, Angular can capture the entered value.
Common Angular Events
Click Events
Execute an action when the user clicks a button or element.
Input Events
Capture values while the user types inside an input field.
Keyboard Events
Handle keyboard actions such as keyup and keydown.
Form Events
Handle form submission and changes in form controls.
Angular Event Binding Syntax
Angular uses parentheses () to bind DOM events.
<button (click)="showMessage()">
Click Me
</button>
(click) → Angular event binding
showMessage() → Component method
Handling Event in Component
The event can call a method defined inside the Angular component class.
showMessage() {
console.log('Welcome to CIIT Training Institute');
}
Using the Event Object
Angular provides the $event object when we need information about the event that occurred.
<input
type="text"
(input)="onInput($event)"
placeholder="Enter your name">
onInput(event: Event) {
const value =
(event.target as HTMLInputElement).value;
console.log(value);
}
Angular can capture the value entered by the user.
Common DOM Events
| Event | Syntax | When It Occurs |
|---|---|---|
| Click | (click) | When an element is clicked |
| Double Click | (dblclick) | When an element is double-clicked |
| Input | (input) | When input value changes while typing |
| Change | (change) | When an input value changes |
| Key Up | (keyup) | When a keyboard key is released |
| Key Down | (keydown) | When a keyboard key is pressed |
| Mouse Enter | (mouseenter) | When mouse enters an element |
| Mouse Leave | (mouseleave) | When mouse leaves an element |
| Submit | (submit) | When a form is submitted |
Complete Example – CIIT Student Selection
Let's create a simple example where CIIT students can be selected using buttons.
<h3>CIIT Training Institute</h3>
<button
(click)="selectStudent('Sam')"
class="btn btn-primary me-2">
Sam
</button>
<button
(click)="selectStudent('Pradnya')"
class="btn btn-primary me-2">
Pradnya
</button>
<button
(click)="selectStudent('Samiksha')"
class="btn btn-primary">
Samiksha
</button>
<p>
Selected Student: {{ selectedStudent }}
</p>
selectedStudent = '';
selectStudent(name: string) {
this.selectedStudent = name;
}
CIIT Training Institute
Select a student:
Event Handling Flow
Event Handling Best Practices
- Keep event handler methods small and readable.
- Use meaningful method names such as selectStudent().
- Use $event when event information is required.
- Avoid putting complex business logic directly inside templates.
- Use appropriate DOM events for the required interaction.
- Keep reusable logic inside services when required.
- Follow consistent naming conventions in Angular components.
Summary
Angular event handling allows applications to respond to user actions.
- Events make Angular applications interactive.
- Parentheses () are used for event binding.
- Methods can be called when events occur.
- $event provides event information.
- Angular supports click, input, keyboard, mouse and form events.
- CIIT Training Institute examples help students understand real-world event handling.