mat-autocomplete panel opening on form load in Angular

1.5k views Asked by At

The first autocomplete control's selection panel is opened when I open modal dialog and I am trying to close the panel as shown below:

html:

<mat-form-field>
  <input
    #autoCompleteInput
    matInput
    type="text"
    [matAutocomplete]="auto"
  >
  <mat-autocomplete #auto="matAutocomplete" autoActiveFirstOption="true" [displayWith]="displayFn">
    <mat-option *ngFor="let option of filteredOptions | async" [value]="option">
      {{option.name}}
    </mat-option>
  </mat-autocomplete>
</mat-form-field>

ts:

//@ViewChild('autoCompleteInput', { read: MatAutocompleteTrigger }) autoComplete: MatAutocompleteTrigger; 
// I also tried this
@ViewChild(MatAutocompleteTrigger) autoCompleteInput: MatAutocompleteTrigger;

ngAfterViewInit() {
  debugger;
  this.autoCompleteInput.closePanel();
}

But unfortunately the first autocomplete's panel is opened on dialog load. How can I prevent the panel opened or if it is not possible, close the panel?

1

There are 1 answers

0
satish e On

just give

autoFocus:false 

this.dialog.open(yourcomponent,{
autoFocus:false
});