我正在创建包含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()
答案 0 :(得分: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
,因为你在任何地方都没有使用它。