import {Component, OnInit} from '@angular/core'; import {FormControl} from '@angular/forms'; import {Observable} from 'rxjs'; import {map, startWith} from 'rxjs/operators'; export interface User { name: string; } /** * @title Display value autocomplete */ @Component({ selector: 'autocomplete-display-example', templateUrl: 'autocomplete-display-example.html', styleUrls: ['autocomplete-display-example.css'], }) export class AutocompleteDisplayExample implements OnInit { myControl = new FormControl(); options: User[] = [ {name: 'Mary'}, {name: 'Shelley'}, {name: 'Igor'} ]; filteredOptions: Observable; ngOnInit() { this.filteredOptions = this.myControl.valueChanges .pipe( startWith(''), map(value => typeof value === 'string' ? value : value.name), map(name => name ? this._filter(name) : this.options.slice()) ); } displayFn(user?: User): string | undefined { return user ? user.name : undefined; } private _filter(name: string): User[] { const filterValue = name.toLowerCase(); return this.options.filter(option => option.name.toLowerCase().indexOf(filterValue) === 0); } }