It is bit late, but you can use it in typescript with making some changes in project.
First you need to install daterangepicker ( you have already install it)
npm install daterangepicker
then make class DateRangePickerDirective
in any folder based on your project structure
import {Directive, ElementRef, OnInit, Input, Output, EventEmitter} from '@angular/core';
@Directive({
selector: '[daterangepicker]'
})
export class DateRangePickerDirective implements OnInit {
@Input() options: Object = {};
@Output() selected: any = new EventEmitter();
constructor(private elementRef: ElementRef) {}
ngOnInit() {
$(this.elementRef.nativeElement)
.daterangepicker(this.options, this.dateCallback.bind(this));
}
dateCallback(start:any, end:any, label:any) {
let message =
[start.format('YYYY-MM-DD') ,end.format('YYYY-MM-DD')]
;
this.selected.emit(message);
}
}
make file name typings.d.ts
in src (or any other folder which is root folder for all ts implementation) and add below code in it
interface JQuery{
daterangepicker(options?: Object, callback?: Function): any;
}
So it will make daterangepicker type to JQuery.
also we need moment and jquery npm package
npm install jquery
npm install moment
add style and scripts in angular.json file if used in angular project
"styles": [
---other styles files-------
"node_modules/daterangepicker/daterangepicker.css"
],
"scripts": [
---other js files---
"./node_modules/jquery/dist/jquery.min.js",
"node_modules/moment/moment.js",
"node_modules/daterangepicker/daterangepicker.js"
],
as daterangepicker has moment and jquery depenedency, we have to follow exact order, so first it loads jquery, then moment and finally when it loads daterangepicker then it doesn't break any where. Also if you have already run project and adding in this file, then you have to start npm process again because it is not working when project is already running.
finally add below line in html file
<input value="" daterangepicker name="start"
(selected)="dateSelected($event)"
[options]="pickerOptions"
class="form-control"/>
add DateRangePickerDirective
in NgModule
declarations
@NgModule({
declarations: [OtherModuleComponent,DateRangePickerDirective],
imports: [CommonModule, NgxPaginationModule, RouterModule.forChild(routes)],
exports: [RouterModule],
})
In .ts file add below code
//import $ as it is not working without importing it and give error on build
import * as $ from "jquery";
pickerOptions: Object = {
'showDropdowns': true,
'showWeekNumbers': true,
'timePickerIncrement': 5,
'autoApply': true,
// 'startDate': '05/28/2016',
// 'endDate': '06/03/2016'
};
dateSelected(message:any) {
let [startDate,endDate] = message;
console.log(startDate);
console.log(endDate);
//perform operations which you have to perform
}
For detail information this whole project will be very helpful.