简体   繁体   English

Angular 2. 移除@Hostlistener()

[英]Angular 2. Remove @Hostlistener()

How can I removed @Hostlistener() in Angular 2, like used removeEventListener in Native JS?我如何在 Angular 2 中删除@Hostlistener() ,就像在 Native JS 中使用removeEventListener一样?

Example: I have many dropDown components in my page.示例:我的页面中有很多下拉组件。 When dropDown opened I want to add handler on document click event and to remove handler when dropDown closed.当 dropDown 打开时,我想在文档点击事件上添加处理程序,并在 dropDown 关闭时删除处理程序。

Native JS:原生JS:

function handler(){
  //do something
}
document.addEventListener('click', handler); // add handler
document.removeEventListener('click', handler); // remove handler

Angular 2:角度 2:

  @HostListener('document: click') onDocumentClick () {
    // do something
  }

  // How can I remove handler?

Julia Passynkova Answer is almost correct. Julia Passynkova 答案几乎是正确的。

Just remove the quotation marks around "document", like this:只需删除“文档”周围的引号,如下所示:

// subscribe
this.handler = this.renderer.listen(document, "click", event =>{...});

// unsubscribe
this.handler();

Annotation:注解:

I find @Smiranin comment quite usefull.我发现@Smiranin 评论非常有用。 As Angular 2 makes use of Rxjs, a better way would be to create a dedicated service for these types of events and expose subjects on it.由于 Angular 2 使用 Rxjs,更好的方法是为这些类型的事件创建专用服务并在其上公开主题。 Components can than consume the subjects event stream resulting in the same behaviour.组件然后可以使用导致相同行为的主题事件流。 This would make the code more decoupled, easier to test and robust to API changes.这将使代码更加解耦,更易于测试并且对 API 更改更健壮。

you probably need manually add/remove listener您可能需要手动添加/删除侦听器

// subscribe
this.handler = this.renderer.listen('document', "click", event =>{...});

// unsubscribe
this.handler();

The best I've managed is to essentially add/remove the method attached to the listener.我所管理的最好的方法是基本上添加/删除附加到侦听器的方法。

First setup the listener:首先设置监听器:

@HostListener('document:click', ['$event'])
handler(event: any) : void {};

Then insert this code as fits for your solution:然后根据您的解决方案插入此代码:

//add handler
this.handler = function(event: any) : void {
    // do something
}

//remove handler
this.handler = function() : void {};

How can I removed @Hostlistener() in Angular 2, like used removeEventListener in Native JS?如何在 Angular 2 中删除@Hostlistener() ,就像在 Native JS 中使用removeEventListener一样?

Example: I have many dropDown components in my page.示例:我的页面中有许多下拉组件。 When dropDown opened I want to add handler on document click event and to remove handler when dropDown closed.当 dropDown 打开时,我想在文档单击事件上添加处理程序,并在 dropDown 关闭时删除处理程序。

Native JS:原生JS:

function handler(){
  //do something
}
document.addEventListener('click', handler); // add handler
document.removeEventListener('click', handler); // remove handler

Angular 2:角度 2:

  @HostListener('document: click') onDocumentClick () {
    // do something
  }

  // How can I remove handler?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM