想要在点击特定按键时触发某个功能吗?

问题描述 投票:0回答:1

我们将根据按键的字符码值触发一个函数。

这是获取按键的字符码值的示例

window:keypress 是一个 javascript 事件。该事件在整个窗口上触发

<div>
<p (window:keypress)="getcharcodeValue()">Click me!</p>
</div>
<p*ngIf="show">show me</p>

例如,我们要触发 Enter 键的 getcharcodeValue() 函数。Enter 键的键码是 13。现在,当我们按下 Enter 键时,该函数将触发整个窗口 初始设置值 oof 显示为 true

triggerFunction(){
this.show = true
}
getcharcodeValue(event:any){
    if(event.keyCode == 13){
      triggerFunction()
}

}

html angular typescript
1个回答
0
投票

您正在寻找主机侦听器,我们可以直接指定我们需要的事件并只需切换标志即可

Stackblitz 演示

© www.soinside.com 2019 - 2024. All rights reserved.