我在IE11中@HostListener有问题。当用户使用keydown事件作为空间时,我需要捕获,我的代码非常简单,它在Chrome和FireFox中工作正常,但在IE中不起作用。
import {Component, HostListener} from '@angular/core';
@Component({
selector: 'home',
styleUrls: ['./home.component.css'],
templateUrl: './home.component.html'
})
export class HomeComponent {
@HostListener('window:keydown.control.space', ['$event'])
@HostListener('window:keydown.space', ['$event'])
spaceEvent(event: any) {
alert('space key!');
}
}
在IE开发人员工具中,我没有看到任何错误或警告,并且我不知道如何解决此问题。有什么建议如何解决这个问题吗?
我找到了解决方案。IE在许多不同的事件中还不能正常工作,需要使用@HostListener('window:keydown', ['$event'])
然后捕获一些keyCode
例:
import {Component, HostListener} from '@angular/core';
@Component({
selector: 'home',
styleUrls: ['./home.component.css'],
templateUrl: './home.component.html'
})
export class HomeComponent {
@HostListener('window:keydown', ['$event'])
spaceEvent(event: any) {
if(event.ctrlKey && event.keyCode == 32)
console.log('ctrl + space');
else if(event.keyCode == 32)
console.log('space');
}
}