javascript - In Angular2 With Bootstrap - Tooltip, Tooltip Need setup by executing javascript, 怎么做?

标签 javascript twitter-bootstrap angular

Angular 2 (2.0.0-rc.4) 我使用 Bootstrap 的 Tooltip,Tooltip 需要在准备好后执行 javascript:

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

在Angular2中,如何执行?

最佳答案

这对我有用:

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');
  }

}

注册:

  1. 将其导入到 app.module.ts 中
  2. 将其添加到@NgModule 的声明中(文件 app.module.ts)

像这样使用它:

<button title="tooltip tilte" [appTooltip]></button>

关于javascript - In Angular2 With Bootstrap - Tooltip, Tooltip Need setup by executing javascript, 怎么做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38188582/

相关文章:

javascript - 制作一个 JQuery 计算器,更改 innerHTML 但没有任何反应

javascript - 提交和值更改事件上的 Angular 4 ControlValueAccessor 值

javascript - 如何在 Angular 7 中使用三个 JS

Angular 2 路由 index.html

javascript - 使用 php 显示多个图像无法使用 jquery 旋转?

javascript - 将一个对象分配给另一个变量

javascript - CSS Border transitions 无限动画循环

jQuery 验证器和 bootstrap.js - 通知/错误

php - 从表 A 的按钮行中收集一行表 B

html - Bootstrap jumbotron 全高没有导航栏的高度