Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

material 2 Autocomplete: select option

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 ?

like image 715
Yoamb Avatar asked Feb 23 '17 23:02

Yoamb


People also ask

How do you clear mat autocomplete when no option is selected from autocomplete dropdown?

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.

How do I use angular material autocomplete?

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.

Can't bind to Matautocomplete since it isn't a known property of input angular?

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.

What is Matautocomplete?

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.


2 Answers

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

like image 168
Martin Schneider Avatar answered Sep 19 '22 03:09

Martin Schneider


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> 
like image 35
Igor Janković Avatar answered Sep 20 '22 03:09

Igor Janković