Skip to content

Commit

Permalink
fix(tooltip): remove native event listener on component destroy
Browse files Browse the repository at this point in the history
Component should clean up events it has registered to.

Closes #4499
  • Loading branch information
Micha Streppel authored and mmalerba committed Jul 6, 2017
1 parent ac3e21a commit cba4b39
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/lib/tooltip/tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -183,8 +186,12 @@ 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());
}
}

Expand All @@ -195,6 +202,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 */
Expand Down

0 comments on commit cba4b39

Please sign in to comment.