错误:Datepicker:DateAdapter未将值识别为日期对象

时间:2017-08-18 09:06:10

标签: javascript angular typescript datepicker angular-material2

从昨天开始,我正在努力解决角材料2 datepicker的问题, 在上次npm install之前它正常工作,现在我得到了:

ERROR Error: Datepicker: value not recognized as a date object by DateAdapter.
HolidayRequestComponent.html:21 
第21行是我<input开始的地方

<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>

我的component.ts:

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

      }
  }

1 个答案:

答案 0 :(得分:7)

根据https://github.com/angular/material2/issues/6265

空字符串不是可接受的值,它会引发以下错误:

UPDATE  b1
SET     b1.defaultguid = b2.defaultguid
FROM    cSC_BusinessUnit b1
        JOIN
        cSC_BusinessUnit b2
            ON b1.ClassName = b2.ClassName
                And b2.BusinessUnitGUID = 5
WHERE   b1.BusinessUnitGUID = 7

所以解决方案就是改变

Datepicker: value not recognized as a date object by DateAdapter.

this.dateRangeForm = new FormGroup({
        holidayDataControl: new FormControl('', Validators.required)
      });