简体   繁体   English

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

[英]How can we select mat option when press on tab key?, it should work like enter button in mat-autocomplete angular 6

How can we select mat option when press on tab key?, it should work like enter button in mat-autocomplete angular 6... In below URL its working when press enter, but whenever we press tab button it should select highlighted option. 按下Tab键时如何选择垫选项?它的工作方式类似于垫自动完成角度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 演示

You can subscribe to this.autoTrigger.panelClosingActions, see stackblitz 您可以订阅this.autoTrigger.panelClosingActions,请参阅stackblitz

If your .html is like 如果您的.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>

In your .ts 在您的.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)
      }
    } )
  }

Update a better aproach (using a directive) in this answer 此答案中 更新更好的方法(使用指令)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 6:如何访问 mat-autocomplete 下拉列表中的所有选项值? - Angular 6: how to access ALL option values in mat-autocomplete dropdown? 如何检测在 Angular 中选择了 mat-autocomplete 选项? - How to detect mat-autocomplete option is selected in Angular? 如何使用 angular 中的 Tab 键从 mat 下拉菜单中选择 select - How to select an option from mat dropdown using Tab key in angular 如何在其他mat-autocomplete更改时绑定mat-autocomplete? - How to bind mat-autocomplete on change of other mat-autocomplete? Angular5 - 如何检测垫子自动完成中的空字段或已删除字段? - Angular5 - How to detect empty or deleted field in mat-autocomplete? 垫子自动完成不允许我 select - mat-autocomplete not allowing me to select 垫子自动完成中的无限滚动 angular 11 - Infinite scroll in mat-autocomplete angular 11 selectTabChange 事件在 mat-tab 加载时触发,因为它应该在加载后选择任何选项卡时触发。 角材料选项卡 - selectTabChange event is firing when mat-tab is loaded as it should be triggered when we select any tab after loading . Angular material tab 如何在 formBuilder 上禁用 mat-autocomplete? - How to disable mat-autocomplete on formBuilder? 如何显示名称但从 mat-autocomplete select 的对象中获取某些其他属性的值? 角-角材料 - How to display a name but take the values of some other property from the object from mat-autocomplete select? Angular- Angular Material
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM