我有一个名为ListComponent的组件,其中包含以下代码。@HostListener("document:keydown",["$event"])handleKeyEvent(event:KeyboardEvent){switch(event.keyCode){case38://uparrowthis.selectPreviousItem();break;case40://downarrowthis.selectNextItem();break;}}当我按下向上箭头键或向下箭头键时,页面上组件的所有实例都会触发事件。我怎样才能只为焦点元素触发事件? 最
这就是我想要做的:import{Directive,HostListener,Input}from'@angular/core';@Directive({selector:'[appResizeWindow]'})exportclassResizeWindowDirective{@Input('appResizeWindow')line_ChartOptions:{};constructor(){}@HostListener('window:resize',['$event'])onResize(event:Event){console.log('Yo');if(event.targe
Angular的HostListener装饰器有两个参数。第一个指定要监听的事件的名称。第二个是可选的字符串数组,名称不明确,名为args。自然地,它的含义目前没有在文档中解释(目前用整个四个词来记录HostListener装饰器,前两个声明它是,呃,“HostListener装饰器”)。我只见过HostListener以两种方式调用:完全省略args参数(例如https://angular.io/guide/styleguide#style-06-01)将args参数指定为['$event'],例如在https://angular-2-training-book.rangle.io/
我正在使用以下方法检测页面上的按键。我的计划是检测何时按下Escape键并在按下时运行一个方法。目前我只是试图记录按下了哪个键。但是,从未检测到Escape键。@HostListener('document:keypress',['$event'])handleKeyboardEvent(event:KeyboardEvent){console.log(event);letx=event.keyCode;if(x===27){console.log('Escape!');}} 最佳答案 尝试使用keydown或keyup事件捕获Es
我正在使用以下方法检测页面上的按键。我的计划是检测何时按下Escape键并在按下时运行一个方法。目前我只是试图记录按下了哪个键。但是,从未检测到Escape键。@HostListener('document:keypress',['$event'])handleKeyboardEvent(event:KeyboardEvent){console.log(event);letx=event.keyCode;if(x===27){console.log('Escape!');}} 最佳答案 尝试使用keydown或keyup事件捕获Es