0

I have a textbox which has autocomplete enabled. Now, I want to retrieve selected option in the textbox from autocomplete suggestions and push it into another textbox. How can I code it in typescript? Is there any method for it?

Below is code for textbox having autocomplete suggestions :

<div class="form-group">
    <label>Primary Contact</label>
    <input type="text" placeholder="Enter" matInput [formControl]="myControl" [matAutocomplete]="auto">
        <mat-autocomplete #auto="matAutocomplete">
            <mat-option *ngFor="let option of options" [value]="option">{{option}}</mat-option>
        </mat-autocomplete>
    </input>
</div>
Tanveer Badar
  • 5,438
  • 2
  • 27
  • 32

2 Answers2

0

mat-autocomplete has different events and in this case you are looking for:

@Output() optionSelected: EventEmitter<MatAutocompleteSelectedEvent>

So do:

<mat-autocomplete ... (optionSelected)="doSomething($event.option.value)">

With that you can assign the data you get in doSomething(), or do whatever you need to do.

STACKBLITZ

AT82
  • 71,416
  • 24
  • 140
  • 167
0

Use ngModel binding to myControl.

<div class="form-group">
    <label>Primary Contact</label>
    <input type="text" placeholder="Enter" matInput [formControl]="myControl" [matAutocomplete]="auto">
        <mat-autocomplete #auto="matAutocomplete">
            <mat-option *ngFor="let option of options" [value]="option">{{option}}</mat-option>
        </mat-autocomplete>
    </input>
</div>
<input type="text" placeholder="displayTextbox" [(ngModel)]="myControl.value" >