I am new to Angular2.
I am trying to implement JQuery ui datepicker in angular2.
I have written a datePicker directive, there i enabled datepicker. Here i am able to select date but finding difficulty to emit selected date to the parent component.
to overcome this i created an object on window object and passing component reference to that object. from there i am calling component function.
I feel this is not a best practice to do.
Can someone help me to do in right way.
import { Directive, ElementRef, Input, NgZone,HostListener,Output,EventEmitter } from '@angular/core';
declare var $:any;
@Directive({
selector: '[uiDatePicker]',
})
export class UiDatePickerDirective {
@Input('uiDatePicker') setDate: string;
@Output() onSelectDate = new EventEmitter();
private el: HTMLElement;
constructor(el: ElementRef,public zone:NgZone) {
this.el = el.nativeElement;
window.angularComponentRef = {
zone: this.zone,
component: this
};
}
doEmitDate(dateText:string){
this.onSelectDate.emit(dateText);
}
ngOnInit() {
$(this.el).datepicker({
onSelect: function(dateText:string) {
window.angularComponentRef.component.doEmitDate(dateText);
}
});
}
}
Here i dont like to use window.angularComponentRef.component object. As it is just storing the reference in global object. this is not good for an application.