码迷,mamicode.com
首页 > 其他好文 > 详细

[Angular] HostListener Method Arguments - Blocking Default Keyboard Behavior

时间:2017-07-23 22:44:01      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:listen   dev   this   fine   hose   from   into   span   out   

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

标签:listen   dev   this   fine   hose   from   into   span   out   

原文地址:http://www.cnblogs.com/Answer1215/p/7226104.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!