Search code examples
angulartimepickerclockpicker

Angular2 ClockPicker


I am currently exploring all alternatives of clockpicker for a web app working with Angular 2

Something like http://weareoutman.github.io/clockpicker/ for exemple.

I have made researches but everything seems not easy to set up.

Has anyone came across any good solution ?

Thanks !


Solution

  • 1st step

    Use angular client (https://github.com/angular/angular-cli) to create a new project:

    ng new clockpicker-example

    2nd step

    Install necessary packages:

    npm install jquery --save-dev

    npm install jquery --save-dev

    npm install clockpicker --save-dev

    npm install @types/clockpicker --save-dev

    3rd step

    Inside .angular-cli.json:

    "styles": [
        "styles.css",
        "../node_modules/bootstrap/dist/css/bootstrap.min.css",
        "../node_modules/clockpicker/dist/bootstrap-clockpicker.min.css"
     ],
     "scripts": [
        "../node_modules/jquery/dist/jquery.min.js",
        "../node_modules/bootstrap/dist/js/bootstrap.min.js",
        "../node_modules/clockpicker/dist/bootstrap-clockpicker.min.js"
      ],
    

    4th step

    create a file called clockpicker.directive.ts

    import {Directive, ElementRef, OnInit} from "@angular/core";
    @Directive({
       selector: '[appClockPicker]'
    })
    
    
    export class ClockPickerDirective implements OnInit {
       constructor(private el: ElementRef) {}
    
       ngOnInit(): void {
         $(this.el.nativeElement).clockpicker({
           placement: 'bottom',
           align: 'left',
           donetext: 'Done',
           afterDone: () => {
             console.log('done');
           }
         });
       }
    }
    

    5th step

    Inside app.component.html:

    <div class="input-group" appClockPicker>
      <input type="text" class="form-control" value="09:30">
      <span class="input-group-addon">
        <span class="glyphicon glyphicon-time"></span>
      </span>
    </div>
    

    6th step

    Inside typings.d.ts add:

    declare var $: any;
    declare var jQuery: any;