没有名称为'dateTime'的表单控件的值访问器

时间:2017-12-26 17:13:02

标签: angular datetimepicker angular-reactive-forms angular4-forms

我正在尝试将外部库应用于日期时间选择器。我收到如下错误:

ERROR Error: Uncaught (in promise): Error: No value accessor for form control with name: 'dateTimeDeparture'
Error: No value accessor for form control with name: 'dateTimeDeparture'

我的代码如下:

模板:

<owl-dateTime-input [(ngModel)]="flightDetails.dateTimeDeparture" name="dateTimeDeparture" [locale]="en" required></owl-dateTime-input>  

组件:

this.en = {
    firstDayOfWeek: 0,
    dayNames: ["Sunday", "Monday", "Tuesday","Wednesday", "Thursday", "Friday", "Saturday"],
    dayNamesShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
    monthNames: [ "January","February","March","April","May","June","July","August","September","October","November","December" ],
    monthNamesShort: [ "Jan", "Feb", "Mar", "Apr", "May", "Jun","Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ]
};

this.flightDetails = {
    dateTimeDeparture: new Date(2017, 8, 10, 13, 30, 30),
    dateTimeArrival: new Date(2017, 8, 10, 13, 30, 30)
};

1 个答案:

答案 0 :(得分:8)

外部库需要ControlValueAccessor才能使用角形式。

尝试像这样添加ngDefaultControl

<owl-dateTime-input [(ngModel)]="flightDetails.dateTimeDeparture" name="dateTimeDeparture" [locale]="en" required formControlName="dateTimeDeparture" ngDefaultControl></owl-dateTime-input>

<owl-dateTime-input [(ngModel)]="flightDetails.dateTimeDeparture" name="dateTimeDeparture" [locale]="en" required ngDefaultControl></owl-dateTime-input>

看这里:

What is ngDefaultControl in Angular?