我们如何在按下 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>
最佳答案
您可以订阅 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/