简体   繁体   English

在 Angular2 中使用 Bootstrap - 工具提示,工具提示需要通过执行 javascript 来设置,怎么做?

[英]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: Angular2 (2.0.0-rc.4) 我使用 Bootstrap 的 Tooltip,Tooltip 需要在准备好后执行 follow javascript:

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

In Angular2,how to execute it?在 Angular2 中,如何执行它?

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在 app.module.ts 中导入
  2. Adding it in declarations on @NgModule (file app.module.ts)将它添加到@NgModule 的声明中(文件 app.module.ts)

And using it like this:并像这样使用它:

<button title="tooltip tilte" [appTooltip]></button>
<div data-toggle="tooltip" #tooltip></div>
class MyComponent {
  @ViewChild('tooltip') tooltip:ElementRef;

  ngAfterViewInit() {
    this.tooltip.nativeElement.tooltip();
  }
}    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM