UNPKG

ipsos-components

Version:

Material Design components for Angular

41 lines (33 loc) 945 B
import {Component} from '@angular/core'; import {FormControl} from '@angular/forms'; import {Observable} from 'rxjs/Observable'; import {startWith} from 'rxjs/operators/startWith'; import {map} from 'rxjs/operators/map'; /** * @title Filter autocomplete */ @Component({ selector: 'autocomplete-filter-example', templateUrl: 'autocomplete-filter-example.html', styleUrls: ['autocomplete-filter-example.css'] }) export class AutocompleteFilterExample { myControl: FormControl = new FormControl(); options = [ 'One', 'Two', 'Three' ]; filteredOptions: Observable<string[]>; 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); } }