Javascript 使用绑定函数删除EventListener

Javascript 使用绑定函数删除EventListener,javascript,angular,ionic-framework,Javascript,Angular,Ionic Framework,我正在听一个类似这样的deviceorientationabsolute事件(在ngonit): 我想停止在Ngondestory上收听该事件。我试过这个: window.removeEventListener("deviceorientationabsolute", this.deviceOrientationEvent); 但我仍然可以在控制台中看到它正在监听事件 我做错了什么?而不是通过窗口进行处理。addEventListener方法您可以使用@HostListener进行处理,如下所

我正在听一个类似这样的
deviceorientationabsolute
事件(在
ngonit
):

我想停止在Ngondestory上收听该事件。我试过这个:

window.removeEventListener("deviceorientationabsolute", this.deviceOrientationEvent);
但我仍然可以在控制台中看到它正在监听事件


我做错了什么?

而不是通过
窗口进行处理。addEventListener
方法您可以使用
@HostListener
进行处理,如下所示

@HostListener('window:deviceorientationabsolute', ['$event'])
deviceOrientationAbsoluteEvent(event) { ... }
当组件被销毁时,它将被自动删除

请参阅:答案了解更多详细信息

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