Passing Data Through Events
Learn how to pass custom values and event information from an Angular template to component methods.
CIIT Training Institute
Event binding can pass useful information to component methods. This is helpful for course selection, enquiry forms, search, filters and other interactive features.
What Does Passing Data Mean?
In Angular, an event can call a component method and pass data along with that method call.
For example, when a user selects Angular Full Stack, the selected course name can be passed to the component method.
Passing a Simple Value
A string, number or other value can be passed directly to a component method.
<button
(click)="selectCourse('Angular Full Stack')">
Angular Full Stack
</button>
selectCourse(course: string) {
console.log(
'Selected Course:',
course
);
}
Passing Multiple Values
Multiple values can be passed to a method when more information is required.
<button
(click)="selectCourse(
'Angular Full Stack',
35000
)">
Join Course
</button>
selectCourse(
course: string,
fees: number
) {
console.log(
'Course:',
course
);
console.log(
'Fees:',
fees
);
}
CIIT Course Selection Example
Let's create a course selection section for CIIT Training Institute.
<h2>
CIIT Training Institute
</h2>
<button
class="btn btn-primary"
(click)="selectCourse('Angular Full Stack')">
Angular Full Stack
</button>
<button
class="btn btn-primary"
(click)="selectCourse('Java Full Stack')">
Java Full Stack
</button>
<button
class="btn btn-primary"
(click)="selectCourse('.NET Full Stack')">
.NET Full Stack
</button>
selectedCourse = '';
selectCourse(course: string) {
this.selectedCourse = course;
}
CIIT Training Institute
Select a course:
Passing the Event Object
Instead of passing a custom value, Angular can pass the actual browser event using $event.
<input
(input)="searchCourse($event)"
placeholder="Search course">
searchCourse(event: Event) {
const input =
event.target as HTMLInputElement;
console.log(
input.value
);
}
Custom Value vs $event
| Approach | Example | Use When |
|---|---|---|
| Custom Value |
(click)="selectCourse('Java')"
|
You already know the required value. |
| Event Object |
(click)="handleClick($event)"
|
You need information about the event. |
| Multiple Values |
(click)="selectCourse(name, fees)"
|
More than one value is required. |
Passing Numbers
Numbers can also be passed to component methods.
<button
(click)="applyDiscount(20)">
Apply 20% Discount
</button>
applyDiscount(discount: number) {
console.log(
'Discount:',
discount + '%'
);
}
Passing Multiple Parameters
<button
(click)="registerCourse(
'Angular Full Stack',
'Online',
35000
)">
Register Now
</button>
registerCourse(
course: string,
mode: string,
fees: number
) {
console.log(
course,
mode,
fees
);
}
This approach is useful when an action needs several related pieces of information.
Complete Example – Course Registration
selectedCourse = '';
selectedMode = '';
registerCourse(
course: string,
mode: string
) {
this.selectedCourse = course;
this.selectedMode = mode;
}
<h2>
CIIT Training Institute
</h2>
<p>
Choose your course mode:
</p>
<button
class="btn btn-primary me-2 mb-2"
(click)="registerCourse(
'Angular Full Stack',
'Online'
)">
Angular - Online
</button>
<button
class="btn btn-primary mb-2"
(click)="registerCourse(
'Angular Full Stack',
'Offline'
)">
Angular - Offline
</button>
<p class="mt-3">
Course:
{{ selectedCourse }}
</p>
<p>
Mode:
{{ selectedMode }}
</p>
CIIT Training Institute
Choose your course mode:
Course: Angular Full Stack
Mode: Online
Passing Data Flow
Best Practices
- Pass only the data required by the event handler.
- Use meaningful parameter names.
- Use proper TypeScript types for method parameters.
- Use $event when actual event information is required.
- Avoid passing unnecessary values from templates.
- Keep event-handler methods simple and readable.
Summary
Angular event binding allows data to be passed from the template to component methods.
We can pass strings, numbers, multiple values or the complete event object using $event.
This technique is useful for interactive features such as CIIT Training Institute course selection, registration, enquiry and filtering.