【问题标题】:Detect keyboard navigation in angular material mat-selection-list检测角材料垫选择列表中的键盘导航
【发布时间】:2018-08-08 07:46:55
【问题描述】:

我想在使用键盘箭头在项目之间导航时执行一些操作。

我应该实施任何事件吗? selectionChange 通过单击 ENTER 触发,但我想在通过箭头导航时激活该功能。

<mat-selection-list #list (selectionChange)="selectionChange($event, list.selectedOptions)">
  <mat-list-option (mouseenter)="showEditIcon(true, i)" (mouseleave)="showEditIcon(false, i)"
                   *ngFor="let lotItem of lotList; let i = index;"
                   (click)="showLotDetails(lotItem, i)"
                   [value]='lotItem'>

【问题讨论】:

    标签: angular angular-material


    【解决方案1】:

    您可以在 mat-selection-list 上使用 keydown 键盘事件来调用您的 selectionChange() 方法。您需要同时添加 (keydown.arrowup)(keydown.arrowdown) 事件处理程序。

    <mat-selection-list #list 
           (selectionChange)="selectionChange($event, list.selectedOptions)"
           (keydown.arrowup)="selectionChange($event)" 
           (keydown.arrowdown)="selectionChange($event)">
    
        <mat-list-option (mouseenter)="showEditIcon(true, i)" (mouseleave)="showEditIcon(false, i)"
                       *ngFor="let lotItem of lotList; let i = index;"
                       (click)="showLotDetails(lotItem, i)"
                       [value]='lotItem'>
    

    这是StackBlitz Demo

    【讨论】:

    • 很好,但事件是键盘事件 - 我怎样才能获得选定\焦点项目的详细信息?
    • 添加这个:&lt;mat-list-option *ngFor="let shoe of typesOfShoes" #opt (keydown)="selected = opt"&gt; - 查看更新的演示。
    • 键盘导航总是选择上一只鞋。因为键盘事件在设置选定的 MatListOption 之前触发。有什么解决办法吗?
    猜你喜欢
    • 1970-01-01
    • 2021-02-17
    • 2018-04-30
    • 1970-01-01
    • 1970-01-01
    • 2020-07-05
    • 2021-05-06
    • 2019-03-11
    • 1970-01-01
    相关资源
    最近更新 更多