You must tell where you can find your jQuery typeScript definition. This can be done in a typings.d.ts file where you put your typings like this :
///<reference path="../typings/jquery/jquery.d.ts" />
Or you can install typings via node and let your configuration load it automatically. This is what angular-cli do.
After that, to use jQuery with Angular2 you must understand a fondamental mechanism. Angular2 has it's own dom representation and jQuery manipulates this dom. That's why people say's "Don't use jQuery with Angular2". It's not right. Angular2 come with a solution for this problem. It calls it ControlValueAccessor. The goal of this, is to alert angular when your jQuery plugin modifies the DOM and also let angular notify your plugin when it modify the DOM too.
Let me explain this through an example of date picker calendar.
import { Directive, ElementRef, OnInit, AfterViewInit, Input, forwardRef, OnDestroy } from '@angular/core';
import { Moment } from 'moment';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
const DATE_PICKER_VALUE_ACCESSOR = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => FnCalendarDirective),
multi: true,
};
@Directive({
selector: '[fn-calendar]',
providers: [DATE_PICKER_VALUE_ACCESSOR],
})
export class FnCalendarDirective implements OnInit, AfterViewInit, ControlValueAccessor, OnDestroy {
@Input() format: string = 'DD/MM/YYYY HH:mm';
@Input() showClose: boolean = true;
@Input() sideBySide: boolean = false;
@Input() ngModel;
private onTouched = () => { };
private onChange: (value: string) => void = () => { };
constructor(private el: ElementRef) {
}
ngOnInit() {
}
ngAfterViewInit() {
$(this.el.nativeElement).datetimepicker({
locale: 'fr',
sideBySide: this.sideBySide,
format: this.format,
showClose: this.showClose,
icons: {
time: 'fa fa-clock-o',
date: 'fa fa-calendar',
up: 'fa fa-chevron-up',
down: 'fa fa-chevron-down',
previous: 'fa fa-chevron-left',
next: 'fa fa-chevron-right',
today: 'fa fa-bullseye',
clear: 'fa fa-trash',
close: 'fa fa-times'
},
}).on('dp.change', event => {
let date: Moment = (<Moment>(<any>event).date);
if (date) {
let value = date.format(this.format);
this.onChange(value);
}
});
this.writeValue(this.ngModel);
}
writeValue(date: string) {
$(this.el.nativeElement).datetimepicker().data('DateTimePicker').date(date);
}
registerOnChange(fn: any) {
this.onChange = fn;
}
registerOnTouched(fn: any) {
this.onTouched = fn;
}
ngOnDestroy() {
$(this.el.nativeElement).datetimepicker().data('DateTimePicker').destroy();
}
}
Important things here, are writeValue method that let Angular2 notify your jquery plugin that a change is producing. And registerOnChange, that let you notify Angular2 that your plugin make a change to the DOM.
In conclusion, the key to use jQuery, is to wrap your jQuery plugin inside directive and to implement a CustomValueAccesor to handle communication between your plugin and Angular2.
To find typings, you can use DefinitlyTyped GitHub which is a repository of typings definition from lot of js library.
[1]: http://blog.thoughtram.io/angular/2016/07/27/custom-form-controls-in-angular-2.html