Angular Material Autocomplete with API Example

By Hardik Savani January 29, 2021 Category : Angular

Hi,

I am going to show you example of angular material autocomplete with api call. i would like to share with you angular material autocomplete with external api. This article will give you simple example of angular material autocomplete web api. it's simple example of angular material input autocomplete api call. follow bellow step for angular material autocomplete with api call.

you can easily use material autocomplete with api call in angular 6, angular 7, angular 8, angular 9, angular 10, angular 11 and angular 12.

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';

import { HttpClientModule } from '@angular/common/http';

@NgModule({

declarations: [

AppComponent

],

imports: [

BrowserModule,

BrowserAnimationsModule,

MatFormFieldModule,

MatInputModule,

MatAutocompleteModule,

FormsModule,

ReactiveFormsModule,

HttpClientModule

],

providers: [],

bootstrap: [AppComponent]

})

export class AppModule { }

Create Service

I will create one service to call api for autocomplete. so let's create as like bellow:

src/app/post.service.ts

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

import { HttpClient } from '@angular/common/http';

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

import { of } from 'rxjs';

@Injectable({

providedIn: 'root'

})

export class PostService {

constructor(private http: HttpClient) { }

opts = [];

getData() {

return this.opts.length ?

of(this.opts) :

this.http.get('https://jsonplaceholder.typicode.com/users').pipe(tap(data => this.opts = data))

}

}

src/app/app.component.ts

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

import {Observable} from 'rxjs';

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

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

import { PostService } from './post.service';

@Component({

selector: 'app-root',

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

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

})

export class AppComponent {

myControl = new FormControl();

options = [];

filteredOptions: Observable;

constructor(private service: PostService) {

this.filteredOptions = this.myControl.valueChanges.pipe(

startWith(''),

debounceTime(400),

distinctUntilChanged(),

switchMap(val => {

return this.filter(val || '')

})

)

}

filter(val: string): Observable {

return this.service.getData()

.pipe(

map(response => response.filter(option => {

return option.name.toLowerCase().indexOf(val.toLowerCase()) === 0

}))

)

}

}

src/app/app.component.html

<h1>angular material input autocomplete with API 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.name">

{{option.name}}

</mat-option>

</mat-autocomplete>

</mat-form-field>

</form>

You can easily run by following command:

ng serve

I hope it can help you...