Search code examples
htmlangularjstypescriptcontrolsng2-bootstrap

How to use new Forms with ng2-bootstrap Timepicker


I am working on creating my own component that wraps the ng2-bootstrap timepicker component. For some reason, I can't seem to get the new Angular 2 FormControl's to work properly, even with using the most recent version of their components which is using @angular/forms.

Here is a link to the component: ng2-bootstrap timepicker

Here is a simple version my code:

import { Component, Input, EventEmitter, OnInit } from '@angular/core';

import { FormControl, FORM_DIRECTIVES } from '@angular/forms';
import { TimepickerComponent } from 'ng2-bootstrap/ng2-bootstrap';

@Component({
  selector: 'my-timepicker',
  directives: [ REACTIVE_FORM_DIRECTIVES, TimepickerComponent ],
  template: `
  <div class="well">
    <timepicker
      [(ngModel)]="initialValue"
      [formControl]="timePickerControl"
      [hourStep]="hourStep"
      [minuteStep]="minuteStep"
      [showMeridian]="showMeridian"
      [readonlyInput]="disabled"
      [mousewheel]="mousewheel"
      [arrowkeys]="arrowkeys"
      [showSpinners]="showSpinners"
      [min]="min"
      [max]="max"
    ></timepicker>
  </div>
`
})
export class TimePickerComponent implements OnInit {
  timePickerControl: FormControl;
  initialValue: Date;
  hourStep: number = 1;
  minuteStep: number = 1;
  showMeridian: boolean = true;
  mousewheel: boolean = true;
  arrowkeys: boolean = true;
  showSpinners: boolean = true;
  min: Date;
  max: Date;

ngOnInit() {
  this.initialValue = new Date();
  this.timePickerControl = new FormControl(this.initialValue, Validators.required);
  }
}

This is the error I am seeing when I try to use the component:

No provider for NgModel ("
<div class="well">
    [ERROR ->]<timepicker
      [(ngModel)]="initialValue"
      [formControl]="timePickerControl"
")

In my app.ts I made sure to include the following calls:

provideForms()
disableDeprecatedForms()

Solution

  • import { FORM_DIRECTIVES } from '@angular/forms';
    import { TimepickerComponent } from 'ng2-bootstrap/ng2-bootstrap';
    
    @Component({
      selector: 'my-timepicker',
      directives: [FORM_DIRECTIVES, TimepickerComponent ],
      template: `
      <div class="well">
        <timepicker
          [(ngModel)]="initialValue"
          [hourStep]="hourStep"
          [minuteStep]="minuteStep"
          [showMeridian]="showMeridian"
          [readonlyInput]="disabled"
          [mousewheel]="mousewheel"
          [arrowkeys]="arrowkeys"
          [showSpinners]="showSpinners"
          [min]="min"
          [max]="max"
          name="aTimePicker"
          required
        ></timepicker>
      </div>
    `
    })
    export class TimePickerComponent implements OnInit {
      initialValue: Date;
      hourStep: number = 1;
      minuteStep: number = 1;
      showMeridian: boolean = true;
      mousewheel: boolean = true;
      arrowkeys: boolean = true;
      showSpinners: boolean = true;
      min: Date;
      max: Date;
    
    ngOnInit() {
      this.initialValue = new Date();
      }
    }
    

    I ran into a similar issue with using the datepicker, I've made a few changes in the snippet above that mirror what I did to get it to work, I haven't tested the change, but let me know how it goes, hopefully its at least a starting point.

    From what I can see you don't need the REACTIVE_FORM_DIRECTIVES currently, as you aren't using it anywhere.