I want to call a function when an option is selected. After some search it seem that i have to use :
property optionSelections of MdAutocompleteTrigger
In the documentation : https://material.angular.io/components/component/autocomplete optionSelections Stream of autocomplete option selections.
I dont understand that , what is a stream, how to implement this ?
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.
Simple autocompleteStart 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.
If 'mat-option' is an Angular component and it has 'value' input, then verify that it is part of this module. If 'mat-option' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule. schemas' of this component to suppress this message.
The <mat-autocomplete>, an Angular Directive, is used as a special input control with an inbuilt dropdown to show all possible matches to a custom query. This control acts as a real-time suggestion box as soon as the user types in the input area.
The Material Autocomplete component has its own optionSelected
event output:
Template:
<mat-autocomplete (optionSelected)="onSelectionChanged($event)"> <mat-option *ngFor="let item of myItems" [value]="item"> {{ item }} </mat-option> </mat-autocomplete>
Controller:
import { MatAutocompleteSelectedEvent } from '@angular/material/autocomplete'; // ... onSelectionChanged(event: MatAutocompleteSelectedEvent) { console.log(event.option.value); }
See: Angular Material API Docs - MatAutocompleteSelectedEvent
On md-option you can set "onSelect"
<md-autocomplete #auto="mdAutocomplete"> <md-option (onSelect)="callSomeFunction()" *ngFor="let state of states" [value]="state.name">{{ state.name }}</md-option> </md-autocomplete>
Since beta 3, functionality has changed:
<md-autocomplete #auto="mdAutocomplete"> <md-option (onSelectionChange)="callSomeFunction()" *ngFor="let state of states" [value]="state.name">{{ state.name }}</md-option> </md-autocomplete>
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