Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mat-autocomplete - How to access selected option?

Tags:

angular

I know that [value] stores the selected Object (Offer in my case). According to materials documentation, optionSelected emits an event. I tried [optionSelected] = "fooFn" but it doesn't exist. I just want to access the Offer object. Thanks!

offer-search.component.html:

  <h5 #offerP>option - autoComplete</h5>   <mat-form-field id="form-field">     <input type="text" matInput [formControl]="myControl" [matAutocomplete]="auto">     <mat-autocomplete #auto="matAutocomplete" [displayWith]="displayFn">       <mat-option *ngFor="let option of filteredOptions$ | async" [value]="option">         {{ option.foodItem.name }}     </mat-option>   </mat-autocomplete>   </mat-form-field> 
like image 648
CCSJ Avatar asked Feb 04 '18 00:02

CCSJ


People also ask

How do you pre select an option in Mat autocomplete?

2 Answers. Show activity on this post. Need to assign a FormControl object on the html input element as shown below. Then in the TS file you can call setValue on the FormControl object to set an initial value.

How do you use autocomplete mat?

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.

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.

What is the default value of the autocomplete?

Default value?… well, if you have a default value, the autocomplete will bring you just one option if you set an object to the input value (the default value selected) or a list of possible objects if it is a string. I’m not sure I’m following you… I suppose you have something like this in your code:

How do I highlight the first autocomplete option when the user opens?

If your use case requires for the first autocomplete option to be highlighted when the user opens the panel, you can do so by setting the autoActiveFirstOption input on the mat-autocomplete component. This behavior can be configured globally using the MAT_AUTOCOMPLETE_DEFAULT_OPTIONS injection token.

Should autocomplete be prefilled or empty input fields?

But if you consider the edit page there should be prefilled value. Which the user can remove and then use autocomplete. If the user doesn’t wants to change the value why empty such an input field. The example above should do it. There’s no need to recreate the input control everytime.

How do I add autocomplete to a form?

The autocomplete is a normal text input enhanced by a panel of suggested options. Start by adding a regular matInput to your template. Let's assume you're using the formControl directive from ReactiveFormsModule to track the value of the input.


2 Answers

You can use it like :

<mat-autocomplete #auto="matAutocomplete" (optionSelected)='getPosts($event.option.value)'> 

WORKING DEMO

like image 177
Vivek Doshi Avatar answered Oct 14 '22 23:10

Vivek Doshi


It can be done in two ways

  1. Using onSelectionChange which emits MatOptionSelectionChange from mat-option
<mat-autocomplete #auto="matAutocomplete">     <mat-option       *ngFor="let option of options"       [value]="option"       (onSelectionChange)="updateMySelection($event)"     >       {{ option }}     </mat-option> </mat-autocomplete> 
  1. Using optionSelected which emits MatAutocompleteSelectedEvent from mat-autocomplete
<mat-autocomplete    #auto="matAutocomplete"   (optionSelected)="updateMySelection($event)">     <mat-option       *ngFor="let option of options"       [value]="option"     >       {{ option }}     </mat-option> </mat-autocomplete> 
like image 41
rainversion_3 Avatar answered Oct 14 '22 22:10

rainversion_3