Angular ng-select2 search on open

1.7k Views Asked by At

i want that the user directly can search when ng-select2 opens. this time the user opens the ng-select2 and needs to click into the searchfield with the mouse and can search - but the search field should get focus directly on opening the ng-select2.

Is there an option to do this? I don't find something on google :(

this is my actual code: HTML:

      <ng-select2 
      [(ngModel)]="AUEEquip"
      (ngModelChange)="filterrole()"
      name="AUEEquip"
      [allowClear]=true
      [data]="equips"
      [options]="options"
      [placeholder]="'Please choose your equipment'"
      style="text-align: left;">
      </ng-select2>

TS:

  options = {
    width: '100%',
    multiple: false,
    tags: false
  };
  equips: Select2OptionData[] = [];
  AUEEquip: string = '0';
1

There are 1 best solutions below

1
Kinglish On

Use @ViewChild

<ng-select2 #chooseInput
      [(ngModel)]="AUEEquip"
      (ngModelChange)="filterrole()"
      name="AUEEquip"
      [allowClear]=true
      [data]="equips"
      [options]="options"
      [placeholder]="'Please choose your equipment'"
      style="text-align: left;">
      </ng-select2>

in component

@ViewChild('chooseInput')
chooseInput: any;

ngOnInit() {

//focus the element
this.chooseInput.nativeElement.focus();

}