Angular Material Autocomplete Select Option Event Example

By Hardik Savani October 20, 2023 Category : Angular

In this example, you will learn angular material autocomplete change event. This post will give you simple example of angular material autocomplete select event. step by step explain angular material autocomplete select option event. We will use angular material autocomplete click event.

you can easily use material autocomplete on change event in angular 6, angular 7, angular 8, angular 9, angular 10, angular 11, angular 12, angular 13, angular 14, angular 15, angular 16 and angular 17.

I will give you one simple example with angular.

Create New App

If you are doing example from scratch then You can easily create your angular app using bellow command:

ng new app-material

Add Material Design

Now in this step, we need to just install material design theme in our angular application. so let's add as like bellow:

ng add @angular/material

Cmd like bellow:

Installing packages for tooling via npm.

Installed packages for tooling via npm.

? Choose a prebuilt theme name, or "custom" for a custom theme: Indigo/Pink

[ Preview: https://material.angular.io?theme=indigo-pink ]

? Set up global Angular Material typography styles? Yes

? Set up browser animations for Angular Material? Yes

src/app/app.module.ts

import { NgModule } from '@angular/core';

import { BrowserModule } from '@angular/platform-browser';

import { AppComponent } from './app.component';

import {FormsModule, ReactiveFormsModule} from '@angular/forms';

import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

import { MatFormFieldModule } from '@angular/material/form-field';

import { MatInputModule } from '@angular/material/input';

import {MatAutocompleteModule} from '@angular/material/autocomplete';

@NgModule({

declarations: [

AppComponent

],

imports: [

BrowserModule,

BrowserAnimationsModule,

MatFormFieldModule,

MatInputModule,

MatAutocompleteModule,

FormsModule,

ReactiveFormsModule

],

providers: [],

bootstrap: [AppComponent]

})

export class AppModule { }

src/app/app.component.html

<h1>angular material input autocomplete onchange event example - ItSolutionStuff.Com</h1>

<form class="example-form">

<mat-form-field>

<input

type="text"

placeholder="Enter Location"

[formControl]="myControl"

matInput

[matAutocomplete]="auto">

<mat-autocomplete #auto="matAutocomplete">

<mat-option

*ngFor="let option of filteredOptions | async"

[value]="option"

(onSelectionChange)="onSelFunc(option)">

{{option}}

</mat-option>

</mat-autocomplete>

</mat-form-field>

</form>

src/app/app.component.ts

import { Component, OnInit } from '@angular/core';

import {Observable} from 'rxjs';

import {map, startWith} from 'rxjs/operators';

import {FormControl} from '@angular/forms';

@Component({

selector: 'app-root',

templateUrl: './app.component.html',

styleUrls: ['./app.component.css']

})

export class AppComponent implements OnInit {

title = 'my-app';

myControl = new FormControl();

options: string[] = [

'Rajkot',

'Surat',

'Vapi',

'Bhavnagar',

'Delhi',

'Mumbai',

'Banglore'

];

filteredOptions: Observable;

ngOnInit() {

this.filteredOptions = this.myControl.valueChanges

.pipe(

startWith(''),

map(value => this._filter(value))

);

}

private _filter(value: string): string[] {

const filterValue = value.toLowerCase();

return this.options.filter(option => option.toLowerCase().includes(filterValue));

}

onSelFunc(option){

console.log(option);

}

}

You can easily run by following command:

ng serve

I hope it can help you...

Shares