Master Angular From Scratch

Learn Angular with clear, interactive, and structured lessons for beginners and developers.

ANGULAR COURSE SYLLABUS
1. Angular Introduction
Angular Introduction Angular History Angular Fundamentals Angular Architecture Component-Based Angular
2. Angular Setup
Angular Setup Introduction Installing Node.js Installing Angular CLI Creating an Angular Application Angular Project Structure
3. Components
Components Overview Component Anatomy Creating Components Component Selector Component Template Component Styles Component Metadata Standalone Components Parent and Child Components Reusable Components
4. Templates
Templates Introduction Template Syntax Template Expressions Template Statements Template Reference Variables ng-template ng-container ngTemplateOutlet Built-in Control Flow
5. Data Binding
Data Binding Introduction Interpolation Binding Property Binding Event Binding Two-Way Data Binding Class & Style Binding
6. Directives
Directives Introduction Attribute Directives Structural Directives Custom Directives
7. Pipes
Pipes Introduction Built-in Pipes Parameterized Pipes Chaining Pipes Custom Pipes
8. Event Handling
Event Handling Introduction DOM Events Event Binding Syntax Event Object Passing Data to Event Handlers
9. Input & Output
Component Communication @Input @Output EventEmitter
10. Lifecycle Hooks
Lifecycle Hooks Introduction ngOnInit ngOnChanges ngAfterViewInit ngOnDestroy
11. Services
Services Introduction Creating Angular Services Using Services in Components Sharing Data Through Services Service Scope
12. Dependency Injection
Dependency Injection Introduction Injecting Dependencies InjectionToken Provider Configuration
13. Routing
Routing Introduction Route Configuration RouterLink & Navigation Route Parameters Child Routes Route Guards
14. Forms
Angular Forms Introduction Template-Driven Forms Reactive Forms Form Validation Form Submission & Error Handling
15. HttpClient
HttpClient Introduction Configuring HttpClient GET Request POST, PUT & DELETE Requests Headers & Parameters HTTP Error Handling
16. Observables
Observables Introduction Creating & Subscribing to Observables Observable Data Streams Observable Operators Subscription Management
17. RxJS
RxJS Introduction RxJS Operators Transformation & Filtering Combining Observables Higher-Order Mapping Subjects
18. API Integration
API Integration Introduction Connecting Angular with REST API Consuming API Data CRUD API Integration API Response & Error Handling

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

1
User Action

The user performs an action on the UI.

→
2
Event

Browser generates an event.

→
3
Binding

Angular connects the event to a handler.

→
4
Component

The component method handles the event.

Main Event Binding Concepts

C
Click Event

Respond to button and element click actions.

I
Input Event

Respond when the value of an input changes.

K
Keyboard Event

Handle keyboard actions such as key presses.

M
Mouse Event

Respond to mouse interactions with elements.

Event Binding Syntax

Event binding uses parentheses around the event name.

Basic Syntax
<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.

Component
export class StudentComponent {

    showMessage() {

        console.log('Button clicked');

    }

}
Template
<button (click)="showMessage()">
    Show Message
</button>
Output:

Event Binding with an Expression

A simple statement can also be written directly inside the event binding.

Example
<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.

Example
<input
    (input)="onInput($event)"
    placeholder="Enter student name">
Component
onInput(event: Event) {

    console.log(
        (event.target as HTMLInputElement).value
    );

}

Event Object

Angular provides the browser event object through the special $event variable.

Template
<button (click)="handleClick($event)">
    Click Me
</button>
Component
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.

Keyup Example
<input
    (keyup)="onKeyUp($event)"
    placeholder="Type something">
Component
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.

Mouse Event Example
<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.

Template
<button
    (click)="selectStudent('Sam')">

    Select Student

</button>
Component
selectStudent(name: string) {

    console.log(
        'Selected: ' + name
    );

}
Example Output:

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

1
Button Actions

Execute component methods when buttons are clicked.

2
Form Input

Respond to changes in input controls.

3
Keyboard Actions

Handle keyboard events from users.

4
Mouse Actions

Respond to mouse interactions.

Complete Event Binding Example

Student Component
export class StudentComponent {

    studentName = 'Sam';
    message = '';

    showStudent() {

        this.message =
            'Student: ' + this.studentName;

    }

}
Student Template
<h2>
    {{ studentName }}
</h2>

<button
    (click)="showStudent()">

    Show Student

</button>

<p>
    {{ message }}
</p>
Student Details

Name: Sam

Student: Sam

Best Practices

1. Use meaningful event handlers

Use clear method names such as showStudent() or submitForm().

2. Keep event handlers simple

Avoid putting large amounts of business logic directly inside template event expressions.

3. Use $event when event information is required

Pass $event when the component needs details about the browser event.

4. Avoid unnecessary event handlers

Add event bindings only when the UI needs to respond to that particular user action.

5. Keep component methods reusable

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.
  • $event provides access to the event object.
  • Event binding generally moves information from the template to the component.
  • Event binding makes Angular applications interactive.