首页 > 代码库 > [Angular] HostListener Method Arguments - Blocking Default Keyboard Behavior
[Angular] HostListener Method Arguments - Blocking Default Keyboard Behavior
We are going to see how to using method arguments for @HostListener.
First, we can use HostListener without method arguments:
@HostListener(‘dblclick‘) toggle(){ this.collapsed = !this.collapsed; }
It works fine.
But if we need to get the $event object and do something with it, for example using preventDefault() from the event object. Then we need to use method arguments:
@HostListener(‘keydown‘, [‘$event‘, ‘$event.keyCode‘]) onKeyDown($event: KeyboardEvent, keyCode) {if(keyCode !== TAB) { $event.preventDefault(); } }
Here we inject ‘$event‘ and ‘$event.keyCode‘ into the method arguments. So for onKeyDown() method, we are able to inject those as well.
[Angular] HostListener Method Arguments - Blocking Default Keyboard Behavior
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。