Remove EventListener with a bind function

amitairos

I'm listening to a deviceorientationabsolute event like this (on ngOnInit):

 this.deviceOrientationEvent = this.onDeviceOrientation.bind(this);
 window.addEventListener("deviceorientationabsolute", this.deviceOrientationEvent);

I want to stop listening to that event on ngOnDestroy. I tried this:

window.removeEventListener("deviceorientationabsolute", this.deviceOrientationEvent);

But I can still see in the console that it's listening to the event.

What am I doing wrong?

Ankit Prajapati

Instead of handling through window.addEventListener method you can handle it using @HostListener as below

@HostListener('window:deviceorientationabsolute', ['$event'])
deviceOrientationAbsoluteEvent(event) { ... }

It will be automatically removed when component will be destroyed

Refer : https://stackoverflow.com/a/41032388/9380944 answer for more details.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related