question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add ability to remove event handlers by name

See original GitHub issue

I’m submitting a…


[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report  
[X] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

No way to remove an element’s event handler without the original eventhandler.

Expected behavior

Ability to remove an element’s event handler by name alone.

Minimal reproduction of the problem with instructions

Consider the following:

@Directive({
    selector: "[disable-click]"
})
export class DisableClickDirective implement OnInit {

    ngOnInit(){
        // here I would like to be able to remove other event listeners according to some input params
    }

    @HostListener("click", ["$event"])
    onClick(event){
        event.preventDefault();
        event.stopPropagation();
        return false;
    }
}

and:

@Component({
    selector: "[my-component]",
    template: `<button (click)="onMyClick($event)" disable-click></button>`
})
export class MyComponent {
    onMyClick(event){
        console.log(event)
    }
}

In this example, I have no way of disabling the previously registered (click) on the host element, so both onClick and onMyClick are triggered (I don’t fully understand why preventDefault doesn’t actually prevent default, but from what I could see when debugging, both onClick and onMyClick were triggered by the same ZoneTask - if it’s a bug, please let me know). If I could remove the click event handlers, that would solve this problem. This is the simplest usage I could think of…

What is the motivation / use case for changing the behavior?

In jQuery we are able to remove an element’s event handlers by name, without knowing the handler. This is very convenient in some cases.

Environment


Angular version: 5.1.2


Browser:
- [X] Chrome (desktop) version 63.0.3239.132
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX
 

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
JiaLiPassioncommented, Feb 19, 2018

@omermizr , in current version of angular

  • you can get all listeners by call (target as any).eventListeners('click')
  • you can remove all listeners by call (target as any).removeAllListeners('click').

But because in the future version of angular, the addEventListener maybe optimized to bypass zone.js patching, so those APIs maybe not available any longer.

0reactions
angular-automatic-lock-bot[bot]commented, Apr 28, 2022

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to remove all event handlers from an event - Stack Overflow
I found a solution on the MSDN forums. The sample code below will remove all Click events from button1 . public partial class...
Read more >
Remove an Event Handler - JavaScript Tutorial
This tutorial shows you how to use the removeEventListener() method to remove an event handler from an event of an element.
Read more >
Event handling (overview) - Event reference - MDN Web Docs
Note: The ability to add and remove event handlers allows you to, for example, have the same button performing different actions in ...
Read more >
JavaScript removeEventListener() method with examples
event: It is a string that describes the name of the event that has to be removed. · listener: It is the function...
Read more >
Removing Event Listeners with removeEventListener()
To reuse elements. Being able to add and remove event listeners allows us to use one element in multiple scenarios. For example, we...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found