javascript - 按下 Tab 键时我们如何选择垫子选项?它应该像垫子自动完成 Angular 6 中的输入按钮一样工作

标签 javascript angular angular-material material-design angular-material2

我们如何在按下 Tab 键时选择 mat 选项?它应该像 mat-autocomplete angular 6 中的 enter 按钮一样工作... 在下面的 URL 中,当按下 Enter 时它可以工作,但每当我们按下 Tab 按钮时,它应该选择突出显示的选项。

<mat-form-field class="example-full-width">
    <input type="text" placeholder="Pick one" aria-label="Number" matInput [formControl]="myControl" [matAutocomplete]="auto">
    <mat-autocomplete #auto="matAutocomplete">
      <mat-option *ngFor="let option of options" [value]="option">
        {{option}}
      </mat-option>
    </mat-autocomplete>
  </mat-form-field>

Demo

最佳答案

您可以订阅 this.autoTrigger.panelClosingActions,参见 stackblitz

如果你的 .html 是这样的

<mat-form-field class="example-full-width">
    <!--see the reference variable-->
    <input #typehead type="text" ...>
    <mat-autocomplete #auto="matAutocomplete">
       ...
    </mat-autocomplete>
</mat-form-field>

在你的 .ts 中

@ViewChild( 'typehead', {read:MatAutocompleteTrigger})  autoTrigger: MatAutocompleteTrigger; 

ngAfterViewInit()
  {
    this.autoTrigger.panelClosingActions.subscribe( x =>{
      if (this.autoTrigger.activeOption)
      {
        console.log(this.autoTrigger.activeOption.value)
        this.myControl.setValue(this.autoTrigger.activeOption.value)
      }
    } )
  }

更新 this answer 中更好的方法(使用指令)

关于javascript - 按下 Tab 键时我们如何选择垫子选项?它应该像垫子自动完成 Angular 6 中的输入按钮一样工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55058305/

相关文章:

javascript - 按下 React-Native 按钮不起作用

php - 无需文件浏览器的花式上传

javascript - 使用 JavaScript 映射和缩减 JSON 对象

javascript - 在 <md-tab-content> 标签下使用 ui-view

angular - 安装 Angular Material 错误包安装失败,见上文

javascript - 访问 JSON 字符串值

javascript - 如何保持 Bootstrap 弹出窗口以便用户可以单击其中的按钮

angular - 覆盖 Angular 默认日期管道

javascript - Angular 2 : Google Maps API Request: No 'Access-Control-Allow-Origin' header is present

javascript - ng-model 绑定(bind)不适用于 ng-template