Skip to content

Instantly share code, notes, and snippets.

@dannypule
Created January 30, 2017 12:26
Show Gist options
  • Save dannypule/fe6c3f18fdc137fad2898e0ea3a3e724 to your computer and use it in GitHub Desktop.
Save dannypule/fe6c3f18fdc137fad2898e0ea3a3e724 to your computer and use it in GitHub Desktop.
@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