Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

have mat-autocomplete not replace but add value

I know that with the onSelectionChange() from the mat-option of the mat-autocomplete i can do something when an option is selected but what i want is for the autocomplete to ADD the value of the mat-option to the input instead of replace its content.

Example:

  • input: "hello"
  • click mat option " world"
  • input: "hello world"

How do i accomplish this without kepping tabs on the form value and stocking its previous value and putting it back (since that just seems like a bad workaround)?

<mat-form-field class="w-100">
   <textarea [matAutocomplete]="auto" [value]="hello" matInput></textarea>
   <mat-autocomplete #auto="matAutocomplete" >
      <mat-option [value]="world">
         world
      </mat-option>
   </mat-autocomplete>
</mat-form-field>
like image 308
lolplayer101 Avatar asked Jan 18 '19 15:01

lolplayer101


1 Answers

My workaround is to always prepend the option value with the current input value

<mat-form-field class="w-100">
   <textarea [matAutocomplete]="auto" [value]="hello" matInput></textarea>
   <mat-autocomplete #auto="matAutocomplete" >
      <mat-option [value]="hello + ' ' + world">  <<=== modified 
         world
      </mat-option>
   </mat-autocomplete>
</mat-form-field>
like image 168
Günter Zöchbauer Avatar answered Sep 23 '22 09:09

Günter Zöchbauer