1

我正在创建自己的组件来包装 ng2-bootstrap timepicker 组件。出于某种原因,我似乎无法让新的 Angular 2 FormControl 正常工作,即使使用使用 @angular/forms 的组件的最新版本也是如此。

这是组件的链接:ng2-bootstrap timepicker

这是我的代码的简单版本:

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);
  }
}

这是我在尝试使用该组件时看到的错误:

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

在我的app.ts我确保包括以下调用:

provideForms()
disableDeprecatedForms()
4

1 回答 1

0
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();
  }
}

我在使用 datepicker 时遇到了类似的问题,我在上面的代码片段中做了一些更改,反映了我为使其工作所做的工作,我没有测试更改,但希望让我知道它是如何进行的它至少是一个起点。

从我所见,您不需要REACTIVE_FORM_DIRECTIVES当前,因为您没有在任何地方使用它。

于 2016-08-10T07:12:55.333 回答