[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.

时间: 2024-10-10 21:46:47

[Angular] HostListener Method Arguments - Blocking Default Keyboard Behavior的相关文章

【PHP系列】PHP推荐标准之PSR-1,PSR-2

说起码代码,刚上大学那会,老师就教导我们,要严格,规范的,把代码写好.代码如人,工工整整.提起规范化的代码,从一开始用命令行编辑C语言代码就开始控制,强制自己按照相关的标准来,所以,现在写代码,不规范都不行,还是为当时打下的好习惯给自己点个赞. 现在写到了PHP,对于PHP,是否也有相关的代码规范呢,当然,你或许在阅读其他博客或者PHP相关文档的时候经常提到这几个名词,PSR-1,PSR-2之类的,这是PHP-FIG制定的推荐规范,今天,我们就来讲解下PHP的推荐标准,PSR(PHP Stand

Default Method in Java 8

Default Methods The section Interfaces describes an example that involves manufacturers of computer-controlled cars who publish industry-standard interfaces that describe which methods can be invoked to operate their cars. What if those computer-cont

angular核心原理解析1:angular自启动过程

angularJS的源代码整体上来说是一个自执行函数,在angularJS加载完成后,就会自动执行了. angular源代码中: angular = window.angular || (window.angular = {}) 定义一个全局的angular空对象. 然后: bindJQuery(); //绑定jQuery publishExternalAPI(angular); //扩展angular对象的方法和属性 jqLite(document).ready(function() { an

angular模块

在angular中,模块可以是一个对象.一个方法或一个数组(数组的最后一个元素必须是方法).后面要讲的模块属性和方法,都是针对通过angular.module()方法定义的模块而言的,我称之为angular模块. 通过angular.module()方法定义的模块是唯一的,如果重复定义,后面的就会覆盖前面的定义.不是通过angular.module()定义的模块,比如一个方法或一个数组,这些模块也是唯一的,但是这些模块一旦定义无法被修改. angular模块可以说是一个空对象,因为它本身不包含任

Mousetrap - a simple library for handling keyboard shortcuts in Javascript

Mousetrap is a simple library for handling keyboard shortcuts in Javascript. It is around 2kb minified and gzipped and 4.5kb minified, has no external dependencies, and has been tested in the following browsers: Internet Explorer 6+ Safari Firefox Ch

Angular Module声明和获取重载

module是angular中重要的模块组织方式,它提供了将一组内聚的业务组件(controller.service.filter.directive…)封装在一起的能力.这样做可以将代码按照业务领域问题分module的封装,然后利用module的依赖注入其关联的模块内容,使得我们能够更好的”分离关注点“,达到更好的”高内聚低耦合“.”高内聚低耦合“是来自面向对象设计原则.内聚是指模块或者对象内部的完整性,一组紧密联系的逻辑应该被封装在同一模块.对象等代码单元中,而不是分散在各处:耦合则指模块.

PatentTips - Object-oriented processor architecture and operating method

BACKGROUND OF THE INVENTION The present invention relates to processors and computer systems. More specifically, the present invention relates to an object-oriented processor architecture and operating method. A conventional central processing unit (

angular之service、factory预provider区别

昨晚项目组做了angular分享,刚好有讨论到这个问题.虽然许久不做前端开发,但是兴趣所致.就查阅了下资料,以便后续需要使用 自己的理解:service是new出来的,factory是直接使用就能获得到service对象,service多了一个this.provider可以初始化注入之前进行一些全局配置,还有就是需要通过$get方法来获得 比较简单的一个理解 app.factory('a', fn); app.service('b', fn); app.provider('c', fn); Th

Supported method argument types Spring MVC

Supported method argument types The following are the supported method arguments: Request or response objects (Servlet API). Choose any specific request or response type, for example ServletRequest or HttpServletRequest. Session object (Servlet API):