从昨天开始,我正在努力解决角材料2 datepicker的问题,
在最后一次npm安装之前,它工作正常,现在我得到:
ERROR Error: Datepicker: value not recognized as a date object by DateAdapter.
HolidayRequestComponent.html:21
第21行是我的<输入开始的地方
<md-input-container [formGroup]="dateRangeForm">
<input
mdInput
name="date_from"
[mdDatepicker]="from"
placeholder="Start date"
formControlName="holidayDataControl"
[ngModel]="date_from"
>
<md-datepicker-toggle mdSuffix [for]="from"></md-datepicker-toggle>
</md-input-container>
<md-datepicker #from></md-datepicker>
我的组件:
import {Component, OnInit, Inject } from '@angular/core';
import {MD_DIALOG_DATA, MdDialog } from '@angular/material';
import {FormBuilder, FormControl, FormGroup, NgForm, Validators} from '@angular/forms';
import {Observable} from 'rxjs/Observable';
@Component({
selector: 'app-holiday-request',
templateUrl: './holiday-request.component.html',
styleUrls: ['./holiday-request.component.css']
})
export class HolidayRequestComponent implements OnInit {
public date_from = new Date();
constructor(@Inject(MD_DIALOG_DATA) public data: any,
private fb: FormBuilder,
) {
this.dateRangeForm = new FormGroup({
holidayDataControl: new FormControl('', Validators.required)
});
}
}
解决方法:
根据https://github.com/angular/material2/issues/6265
空字符串不是可接受的值,它会引发以下错误:
Datepicker: value not recognized as a date object by DateAdapter.
所以解决方案就是改变
this.dateRangeForm = new FormGroup({
holidayDataControl: new FormControl('', Validators.required)
});
至
this.dateRangeForm = new FormGroup({
holidayDataControl: new FormControl(null, Validators.required)
});