防止使用SPACE键切换席子扩展面板

时间:2019-06-03 06:57:32

标签: angular

我尝试了下面的代码,onFilterKeyboard也被调用,但SPACE键仍然有效

<mat-expansion-panel-header collapsedHeight="40px" expandedHeight="40px" (keydown)="onFilterKeyboard($event);">
                        <mat-panel-title (keydown)="onFilterKeyboard($event);">
                            <div (keydown)="onFilterKeyboard($event);">{{aFilter.name}}</div>
                        </mat-panel-title>
    </mat-expansion-panel-header>

在Js:

onFilterKeyboard(event) {
        event.preventDefault();
        event.stopImmediatePropagation();
        event.stopPropagation();
    }

2 个答案:

答案 0 :(得分:0)

您可以尝试将其添加到组件的constructorngOnInit中:

document.addEventListener('keydown', (event) => {
  if (event.keyCode == 32) { // 32 is space bar key code
    event.stopPropagation();
  }
});

答案 1 :(得分:0)

(keydown.Space)=“ onFilterKeyboard($ event);”工作了。 它将创建一个伪事件。 有关伪事件的更多信息,请参见https://medium.com/claritydesignsystem/angular-pseudo-events-d4e7f89247ee