Created
January 30, 2017 12:26
-
-
Save dannypule/fe6c3f18fdc137fad2898e0ea3a3e724 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Directive({ selector: '[my-tooltip]' }) | |
export class TooltipDirective implements OnDestroy { | |
@Input() tooltipTitle: string = ''; | |
private id: string; | |
constructor(private tooltipService: TooltipService, private element: ElementRef) { } | |
@HostListener('mouseenter') | |
onMouseEnter(): void { | |
this.id = Math.random(); | |
this.tooltipService.components.push({ | |
id: this.id, | |
ref: this.element, | |
title: this.tooltipTitle | |
}); | |
} | |
@HostListener('mouseleave') | |
onMouseLeave(): void { | |
this.destroy(); | |
} | |
ngOnDestroy(): void { | |
this.destroy(); | |
} | |
destroy(): void { | |
const idx = this.tooltipService.components.findIndex((t) => { | |
return t.id === this.id; | |
}); | |
this.tooltipService.components.splice(idx, 1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment