本文介绍了Angular 2全局密钥检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用RC5在文档上绑定文档上的keyevent侦听器而不是Angular 2中的特定输入字段?
例如:
我知道这个绑定到元素
$ b
< input(keypress)=onKeyDown($ event)[(ngModel) ] =somethingtype =text>
我如何将它绑定到文档,例如
< div(keypress)=onKeyDown($ event)> < input /> ...< / div>
解决方案
@HostListener('window:keydown',['$ event'])
onKeyDown(event){
...
}
您也可以
< div(window:keypress)=onKeyDown($ event)>
或
< div(document)=onKeyDown($ event)>
声明式过滤就像
< div(window:keydown.alt.a)=onKeyDown($ event)>
当前不支持全局侦听器
另见
How can I bind a keyevent listener on the document instead of an specific inputfield in Angular 2 using RC5?
For example:
I Know this "bind it to an element"
<input (keypress)="onKeyDown($event)" [(ngModel)]="something" type="text">
How can I bind it to the document for example
<div (keypress)="onKeyDown($event)"> <input /> ... </div>
解决方案
@HostListener('window:keydown', ['$event'])
onKeyDown(event) {
...
}
You can also do
<div (window:keypress)="onKeyDown($event)">
or
<div (document)="onKeyDown($event)">
Declarative filtering like
<div (window:keydown.alt.a)="onKeyDown($event)">
is currently not supported for global listeners
See also https://github.com/angular/angular/issues/7308
这篇关于Angular 2全局密钥检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!