1

How is the best way to use a bootstrap plugin like Eonasdan/bootstrap-datetimepicker in Angular2?

I use jspm in my setup and have installed Eonasdan/bootstrap-datetimepicker. The I include the javascript in my Component:

import { datepicker } from 'Eonasdan/bootstrap-datetimepicker';

In AfterViewInit I have tried to add the .datetimepicker() to the html element like this:

ngAfterViewInit() {
      var elm = <HTMLDivElement>document.getElementById('datetimepicker1');
      elm.datetimepicker();
    }

But this is not working. How can I solve this issue?

5
  • Can you provide a plunker? Commented Mar 17, 2016 at 6:51
  • How about $(elm).datetimepicker(); ? Commented Mar 17, 2016 at 8:32
  • you want to use datePIcker in angular2 ? i have another custom date picker if you want ill post it as answer. Commented Mar 17, 2016 at 8:41
  • @PardeepJain I would really like to see the picker if you like :) Commented Mar 17, 2016 at 8:48
  • @DNRN i have posted answer check it out. Commented Mar 17, 2016 at 8:59

2 Answers 2

4

Instead of leaving a comment I prefer to post this as answer (as demanded by @DNRN) because it may helpful to someone else in the future.

You test the solution without having to import any extra files, I have posted a solution online using the datepicker component and bootflat as a styling framework as well as a date picker library or for more info you can refer to this link...

enter image description here enter image description here

Here are few More best articles/libraries for datepicker

Sign up to request clarification or add additional context in comments.

Comments

0

I have just created a directive for Eonasdan/bootstrap-datetimepicker in Angular 2: https://github.com/atais/ng2-eonasdan-datetimepicker

It is currently in very early stages but hopefully it should meet your requirements.

Regarding your question, I have managed to instantiate the calendar element with the help of jQuery:

import * as $ from 'jquery';

constructor(el: ElementRef, renderer: Renderer) {
    let $parent = $(el.nativeElement.parentNode);
    this.dpElement = $parent.hasClass('input-group') ? $parent : $(el.nativeElement);
}

ngOnInit(): void {
    this.dpElement.datetimepicker(this.options);
    this.dpElement.data('DateTimePicker').date(this.date);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.