Event Object in Angular
Learn how Angular provides the event object and how to access information about the event that occurred.
CIIT Training Institute
The event object helps Angular applications understand what happened during a user interaction, such as which key was pressed or what value was entered.
What is an Event Object?
When a DOM event occurs, the browser creates an object containing information about that event.
Angular provides this object through the $event variable.
The $event Variable
Angular provides the special $event variable inside an event binding expression.
<button (click)="handleClick($event)">
Click Here
</button>
handleClick(event: MouseEvent) {
console.log(event);
}
Mouse Event Object
Mouse events provide information such as the clicked element, mouse position and button information.
<button
(click)="showMouseEvent($event)">
Click for Event Details
</button>
showMouseEvent(event: MouseEvent) {
console.log('Event Type:', event.type);
console.log('Mouse X:', event.clientX);
console.log('Mouse Y:', event.clientY);
}
Input Event Object
The input event object can be used to access the value entered by the user.
<input
type="text"
(input)="courseSearch($event)"
placeholder="Search Angular course">
courseSearch(event: Event) {
const value =
(event.target as HTMLInputElement).value;
console.log(value);
}
The entered value can be accessed through $event.
Keyboard Event Object
Keyboard events provide information about the key pressed by the user.
<input
(keydown)="checkKey($event)"
placeholder="Press any key">
checkKey(event: KeyboardEvent) {
console.log(
'Pressed Key:',
event.key
);
}
event.key
Returns the key that was pressed.
event.keyCode
Represents keyboard information available from the event.
event.type
Returns the type of the event.
event.target
Identifies the element that generated the event.
Accessing the Event Target
The target property identifies the HTML element that generated the event.
handleInput(event: Event) {
const input =
event.target as HTMLInputElement;
console.log(input.value);
}
In this example, the event target is converted into an HTMLInputElement so that its value property can be accessed safely.
Common Event Object Properties
| Property | Purpose | Example |
|---|---|---|
type |
Returns event type | click |
target |
Returns event source element | input |
key |
Returns pressed keyboard key | Enter |
clientX |
Mouse X coordinate | 450 |
clientY |
Mouse Y coordinate | 280 |
Complete Example – CIIT Course Enquiry
Let's create a course enquiry example for CIIT Training Institute.
<h2>
CIIT Training Institute
</h2>
<input
type="text"
(input)="onCourseSearch($event)"
placeholder="Enter course name">
<p>
Searching:
{{ searchText }}
</p>
<button
(click)="submitEnquiry($event)">
Submit Enquiry
</button>
searchText = '';
onCourseSearch(event: Event) {
const input =
event.target as HTMLInputElement;
this.searchText = input.value;
}
submitEnquiry(event: MouseEvent) {
console.log(
'Enquiry submitted'
);
console.log(
'Event:',
event.type
);
}
CIIT Training Institute
Searching: Angular Full Stack
Event Object Flow
Event Object Best Practices
- Use $event only when event information is required.
- Use appropriate TypeScript event types such as MouseEvent and KeyboardEvent.
- Convert event.target to the required HTML element type.
- Keep event-processing logic inside component methods.
- Avoid unnecessary access to the complete event object.
- Use meaningful method names for event handlers.
Summary
Angular provides the $event object to access information about a DOM event.
Important event properties include type, target, key, clientX and clientY.
Understanding the event object helps developers build interactive applications such as course enquiry forms, registration forms and dashboards at CIIT Training Institute.