Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save on (focusout) with angular mat-autocomplete

I have a simple input with autocomplete

<input [matAutocomplete]="auto" (focusout)="save()">
<mat-autocomplete #auto="matAutocomplete">
   <mat-option *ngFor="let number of numbers" [value]="number">
     {{number}}
   </mat-option>
</mat-autocomplete>

I want to save the input value when the user is no longer using the input field

The issue is when i am clicking on a autocomplete option focusout function of input field is called.

What are my options to save input value which checks both autocomplete and input field status

Thankyou

like image 200
Nihal Reddy Avatar asked Jan 24 '26 20:01

Nihal Reddy


1 Answers

If you ONLY want to handle the clicking on autocomplete option you should use (click) on that option to handle it.

<input [matAutocomplete]="auto">
<mat-autocomplete #auto="matAutocomplete">
   <mat-option (click)="save(number)" *ngFor="let number of numbers" [value]="number">
     {{number}}
   </mat-option>
</mat-autocomplete>

However, if you want to do it with focus, this will work:

<input #myInput (focusout)="save(myInput.value)" [matAutocomplete]="auto">
<mat-autocomplete #auto="matAutocomplete">
   <mat-option *ngFor="let number of numbers" [value]="number">
     {{number}}
   </mat-option>
</mat-autocomplete>

Stackblitz

like image 149
Dino Avatar answered Jan 28 '26 22:01

Dino



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!