【问题标题】:How can we select mat option when press on tab key?, it should work like enter button in mat-autocomplete angular 6按下tab键时如何选择mat选项?,它应该像mat-autocomplete angular 6中的输入按钮一样工作
【发布时间】:2019-07-30 04:42:43
【问题描述】:

我们如何在按下 tab 键时选择 mat 选项?,它应该像 mat-autocomplete angular 6 中的输入按钮一样工作... 在下面的 URL 中,当按下回车键时它会工作,但每当我们按下 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

【问题讨论】:

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


    【解决方案1】:

    您可以订阅 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中的更好方法(使用指令)

    【讨论】:

    • 感谢您的回复..您发送的演示无法正常工作...按下Tab键时未选择mat选项...
    • 它对我有用(在 fireFox 和 Chrome 中),你需要一些选项是 higligth(使用箭头键选择)。
    猜你喜欢
    • 1970-01-01
    • 2020-02-11
    • 2023-03-21
    • 1970-01-01
    • 2019-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-19
    相关资源
    最近更新 更多