Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Angular2 With Bootstrap - Tooltip, Tooltip Need setup by executing javascript, How to do it?

Angular2 (2.0.0-rc.4) I use Bootstrap's Tooltip, Tooltip need execute follow javascript when ready:

$(function () {
  $('[data-toggle="tooltip"]').tooltip()
})

In Angular2,how to execute it?

like image 231
YETI Avatar asked Jul 23 '26 00:07

YETI


1 Answers

That worked for me:

import { Directive, ElementRef, Input, HostListener, OnDestroy } from '@angular/core';

declare var $: any;

@Directive({
  selector: '[appTooltip]'
})
export class TooltipDirective implements OnDestroy {


  @Input()
  public appTooltip: string;

  constructor(private elementRef: ElementRef) { }

  @HostListener('mouseenter')
  public onMouseEnter(): void {
    const nativeElement = this.elementRef.nativeElement;
    $(nativeElement).tooltip('show');
  }

  @HostListener('mouseleave')
  public onMouseLeave(): void {
    const nativeElement = this.elementRef.nativeElement;
    $(nativeElement).tooltip('dispose');
  }


ngOnDestroy(): void {
    const nativeElement = this.elementRef.nativeElement;
    $(nativeElement).tooltip('dispose');
  }

}

registering:

  1. Importing it in in app.module.ts
  2. Adding it in declarations on @NgModule (file app.module.ts)

And using it like this:

<button title="tooltip tilte" [appTooltip]></button>
like image 79
yonexbat Avatar answered Jul 25 '26 13:07

yonexbat