我正在创建自己的组件来包装 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()