import {Component, OnInit} from '@angular/core'; import {FormControl} from '@angular/forms'; import {Observable} from 'rxjs'; import {map, startWith} from 'rxjs/operators'; /** * @title Highlight the first autocomplete option */ @Component({ selector: 'autocomplete-auto-active-first-option-example', templateUrl: 'autocomplete-auto-active-first-option-example.html', styleUrls: ['autocomplete-auto-active-first-option-example.css'] }) export class AutocompleteAutoActiveFirstOptionExample implements OnInit { myControl: FormControl = new FormControl(); options = ['One', 'Two', 'Three']; filteredOptions: Observable; ngOnInit() { this.filteredOptions = this.myControl.valueChanges.pipe( startWith(''), map(val => this.filter(val)) ); } filter(val: string): string[] { return this.options.filter(option => option.toLowerCase().indexOf(val.toLowerCase()) === 0); } }