I did it by creating a directive.
import { Directive, OnInit, Inject, ElementRef } from '@angular/core';
import { JQUERY_TOKEN } from './jquery.service';
@Directive({
selector: '[data-toggle="tooltip"]'
})
export class TooltipDirective implements OnInit {
private el: HTMLElement;
constructor(elRef: ElementRef, @Inject(JQUERY_TOKEN) private $: JQueryStatic) {
this.el = elRef.nativeElement;
}
ngOnInit() {
this.$(this.el).tooltip();
}
}
This is my jquery.service
file mentioned in the import
statement
import { InjectionToken } from '@angular/core';
export const JQUERY_TOKEN = new InjectionToken<JQueryStatic>('jQuery');
Then add to the module
import { NgModule } from '@angular/core';
import { TooltipDirective } from './tooltip.directive';
import { JQUERY_TOKEN } from './jquery.service';
export let jQuery: JQueryStatic = window['jQuery'];
@NgModule({
imports: [
// ...
],
declarations: [
TooltipDirective
],
providers: [
{ provide: JQUERY_TOKEN, useValue: jQuery }
]
})
export class MyModule {}
Then simply add it to the component where you need it. For example, in my app.component.ts
file, all I had to do is add this one line at the top to get it to work with the template:
import { TooltipDirective } from './tooltip.directive';