From 63f25114eeadf6e06734a2e21dbdf917b73c975c Mon Sep 17 00:00:00 2001 From: michastreppel Date: Fri, 7 Jul 2017 19:06:22 +0200 Subject: [PATCH] fix(tooltip): remove native event listener on component destroy (#5144) * fix(tooltip): remove native event listener on component destroy Component should clean up events it has registered to. Closes #4499 * Break at higher syntactic level * Undo accidental formatting --- src/lib/tooltip/tooltip.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/lib/tooltip/tooltip.ts b/src/lib/tooltip/tooltip.ts index 261b60a381b4..a2692b157c12 100644 --- a/src/lib/tooltip/tooltip.ts +++ b/src/lib/tooltip/tooltip.ts @@ -170,6 +170,9 @@ export class MdTooltip implements OnDestroy { get _matClass() { return this.tooltipClass; } set _matClass(v) { this.tooltipClass = v; } + private _enterListener: Function; + private _leaveListener: Function; + constructor( private _overlay: Overlay, private _elementRef: ElementRef, @@ -183,8 +186,10 @@ export class MdTooltip implements OnDestroy { // The mouse events shouldn't be bound on iOS devices, because // they can prevent the first tap from firing its click event. if (!_platform.IOS) { - _renderer.listen(_elementRef.nativeElement, 'mouseenter', () => this.show()); - _renderer.listen(_elementRef.nativeElement, 'mouseleave', () => this.hide()); + this._enterListener = + _renderer.listen(_elementRef.nativeElement, 'mouseenter', () => this.show()); + this._leaveListener = + _renderer.listen(_elementRef.nativeElement, 'mouseleave', () => this.hide()); } } @@ -195,6 +200,11 @@ export class MdTooltip implements OnDestroy { if (this._tooltipInstance) { this._disposeTooltip(); } + // Clean up the event listeners set in the constructor + if (!this._platform.IOS) { + this._enterListener(); + this._leaveListener(); + } } /** Shows the tooltip after the delay in ms, defaults to tooltip-delay-show or 0ms if no input */