-1

Trying to get the closed event of mat autocomplete but getting undefined. I do not know why i am getting undefined. If anyone knows please help to find the solution.

app.component.html:

    <mat-autocomplete #auto="matAutocomplete" (closed)="onClosedEvent($event)" >
    <mat-option *ngFor="let animal of animals"
        [value]="animal.name">
    {{animal.name}}
    </mat-option>
    </mat-autocomplete>

app.component.ts:

  onClosedEvent(event) {
     console.log(event);
  }

Demo: https://stackblitz.com/edit/angular-rgqlgr?file=src%2Fapp%2Fapp.component.ts

EMahan K
  • 417
  • 1
  • 19

2 Answers2

0

This is because, as per documentation, closed event doesn't emit any value. It just informs that the drop-down panel was closed. Note the void type on EventEmitter.

If you want to know which option was selected, you can use the optionSelected event, which emits the MatAutocompleteSelectedEvent.

Also note that I've linked the v7 documentation since this is what you were using in the stackblitz. The API had some changes between v7 and the latest (v14), so take that into consideration.

TotallyNewb
  • 3,884
  • 1
  • 11
  • 16
0

you can use

 onClosedEvent() {
  console.log(this.myGroup.get('identifier').value);
  console.log(this.myGroup.get('identifier2').value);
 }
RED-ONE
  • 167
  • 1
  • 5