I'm using bootstrap-datepicker in my Angular project by creating it as a directive. Below is my code.
HTML:
<input [datepicker]="datepickerConfig" readonly ngModel name="requestedDate" class="form-control" id="requestedDate" type="text">
Datepicker config in component:
datepickerConfig = {
format: 'dd-M-yyyy'
};
Directive:
@Directive({ selector: '[datepicker]' })
export class DatepickerDirective implements OnInit {
@Input() datepicker;
constructor(private el: ElementRef) { }
ngOnInit() {
$(this.el.nativeElement).datepicker(this.datepicker);
$(this.el.nativeElement).next('.input-group-addon').find('.glyphicon-calendar')
.click(() => $(this.el.nativeElement).focus());
}
}
If I focus on the textbox to which I've applied this directive, the datepicker pops up, and when I select a date, it's shown on the textbox. But it's not getting bound to the underlying ngModel
/ formControlName
. The corresponding variable is still undefined
.
Please help me with this.
I did it using
ControlValueAccessor
. Below is my implementation.