import { MdAutocomplete } from '@angular/material';
@Component({
template:'
<input type="text" [mdAutocomplete]="auto"/>
<md-autocomplete #auto="mdAutocomplete" #autoComplete>
<md-option>
Some Options
</md-option>
</md-autocomplete>'
})
export class AutoComplete {
@ViewChild('autoComplete') autoComplete: MdAutocomplete;
closeAuto() {
this.autoComplete.closePanel();
}
}
It is not the complete code but the idea is to call closePanel inside a method. closePanel is listed as a method on https://material.angular.io/components/autocomplete/api but it fails to work. It says method not found.
tried this approach too
import { MdAutocompleteTrigger } from '@angular/material';
@Component({
template:'
<input #autoCompleteInput type="text" [mdAutocomplete]="auto"/>
<md-autocomplete #auto="mdAutocomplete" #autoComplete>
<md-option>
Some Options
</md-option>
</md-autocomplete>'
})
export class AutoComplete {
@ViewChild('autoCompleteInput') autoComplete: MdAutocompleteTrigger;
closeAuto() {
this.autoComplete.closePanel();
}
}
Start by creating the autocomplete panel and the options displayed inside it. Each option should be defined by a mat-option tag. Set each option's value property to whatever you'd like the value of the text input to be when that option is selected.
The Angular Autocomplete is a textbox or search box component that provides a list of suggestions to select from as the user types. It has several out-of-the-box features such as data binding, filtering, grouping, autocomplete search, UI customization, accessibility, and more.
Answers 1 : of How to clear mat- autocomplete when no option is selected from autocomplete dropdown. You can remove the formControl-binding from your input and when you select an option you set that id to your form.
Angular reads ElementRef
by default from html element if you don't specify read
option.
So
template
<input #autoCompleteInput type="text" [matAutocomplete]="auto"/>
component
@ViewChild('autoCompleteInput', { read: MatAutocompleteTrigger })
autoComplete: MatAutocompleteTrigger;
Here is the Plunker Example that demonstrates this approach.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With