Javascript Angular 2@HostListener未捕获keyup事件
我有一个谷歌地图,它应该可以捕捉按键事件。我的组件是google maps div的父组件。它在Chrome上运行良好,而在Firefox或IE上运行不好(未通过Edge测试) 我的组成部分:Javascript Angular 2@HostListener未捕获keyup事件,javascript,angular,Javascript,Angular,我有一个谷歌地图,它应该可以捕捉按键事件。我的组件是google maps div的父组件。它在Chrome上运行良好,而在Firefox或IE上运行不好(未通过Edge测试) 我的组成部分: @Component({ selector: 'dashboard', templateUrl: 'dashboard.component.html', styleUrls: ['dashboard.component.css'], host: { '[attr.tabindex
@Component({
selector: 'dashboard',
templateUrl: 'dashboard.component.html',
styleUrls: ['dashboard.component.css'],
host: {
'[attr.tabindex]': '-1'
},
providers: []
})
export class DashboardComponent implements OnInit{
/* some functions and other stuff here
* ...
*/
@HostListener('keyup', ['$event']) keyup(e) {
console.log('This will be called with Chrome, not with the others.');
}
};
你也有过同样的经历吗?(如果需要更多信息,请告诉我)。
谢谢
[编辑]我尝试通过使用ElementRef捕捉
keyup
事件,并将我的事件处理程序设置为ElementRef.nativeElement
的onkeyup
属性,但运气不佳,因为某些原因,Chrome会将keyup事件分配给父浏览器,而其他浏览器则不会。您可以尝试直接订阅地图的事件
@ViewChild('myMap')
map: ElementRef;
constructor(private renderer: Renderer) {};
ngOnInit() {
this.renderer.listen(this.map.nativeElement, 'keyup', (event) => {
// ...
});
}
在模板中:
<div #myMap>...</div>
。。。
尝试收听窗口:keyup
或文档:keyup
事件,而不是简单地收听keyup
有时
窗口:keyup
事件无法与@HostListener
一起工作,尽管它在同一应用程序的其他情况下也能工作。尝试改用文档:keyup
事件。不幸的是,这不起作用。我添加了tabindex=“-1”
。它仍然适用于Chrome。