Angular

Angular Change Event

Angular Change Event

The Angular change event is a DOM event that triggers when users commit changes to form elements like <input>, <select>, and <textarea>. This Angular form event fires when the element loses focus after being modified, making it essential for event handling in Angular applications.

When Does Angular Change Event Fire

The Angular change event triggers in these scenarios:

  • Text inputs: User types and moves focus away (blur occurs)
  • Select dropdowns: User selects a new option via mouse or keyboard
  • Checkboxes: User toggles the checkbox state through interaction

This Angular form event is perfect for input validation and form submission handling.

Basic Angular Change Event Example

Here’s how to implement the Angular change event with input elements:

// filepath: src/app/app.component.html
<div>
    <label>Enter Text: </label>
    <input type="text" (change)="onChange($event)" />
</div>
// filepath: src/app/app.component.ts
import { Component } from '@angular/core';

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html'
})
export class AppComponent {
    onChange(event: Event): void {
        const target = event.target as HTMLInputElement;
        console.log('Value changed:', target.value);
    }
}

Angular Change Event with Select Dropdown

Handle dropdown changes using the Angular change event:

// filepath: src/app/app.component.html
<select (change)="onSelectChange($event)">
    <option value="">Choose option</option>
    <option value="option1">Option 1</option>
    <option value="option2">Option 2</option>
</select>
// filepath: src/app/app.component.ts
onSelectChange(event: Event): void {
    const target = event.target as HTMLSelectElement;
    console.log('Selected:', target.value);
}

Checkbox Angular Change Event Handling

Use the Angular change event for checkbox interactions:

// filepath: src/app/app.component.html
<label>
    <input type="checkbox" (change)="onCheckboxChange($event)">
    Accept terms
</label>
// filepath: src/app/app.component.ts
onCheckboxChange(event: Event): void {
    const target = event.target as HTMLInputElement;
    console.log('Checked:', target.checked);
}

The Angular change event is the ideal choice for form validation and handling user input changes in Angular applications. This event listener ensures your Angular forms respond appropriately when users complete their input modifications.

Try the working Angular change event example on StackBlitz


🚀 Welcome to CodePro – Your Ultimate Coding Hub!

Embark on a coding journey with CodePro, your destination for insightful tutorials on Angular, React, JavaScript, HTML, CSS, and Blazor. From tackling event handling to crafting interactive web elements, we’ve got your coding adventure covered. Subscribe now for a front-row seat to the dynamic world of web development!

Visit our YoutTube: CodeLSC Channel and website: CodePro Blog for additional resources, articles, and more!