Angular Material Autocomplete with API Example

By Hardik Savani | January 29, 2021 | Category : Angular


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 and angular 11.

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: ]

? Set up global Angular Material typography styles? Yes

? Set up browser animations for Angular Material? Yes


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


declarations: [



imports: [










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:


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

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

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

import { of } from 'rxjs';


providedIn: 'root'


export class PostService {

constructor(private http: HttpClient) { }

opts = [];

getData() {

return this.opts.length ?

of(this.opts) :

this.http.get('').pipe(tap(data => this.opts = data))




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


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(




switchMap(val => {

return this.filter(val || '')




filter(val: string): Observable {

return this.service.getData()


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

return === 0






<h1>angular material input autocomplete with API example - ItSolutionStuff.Com</h1>

<form class="example-form">




placeholder="Enter Location"




<mat-autocomplete #auto="matAutocomplete">


*ngFor="let option of filteredOptions | async"







You can easily run by following command:

ng serve

I hope it can help you...

Hardik Savani
My name is Hardik Savani. I'm a full-stack developer, entrepreneur and owner of Aatman Infotech. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.
Follow Me: Github Twitter
***Do you want me hire for your Project Work? Then Contact US.

We are Recommending you: