2017-08-18 44 views
3

從昨天值我與與角料2日期選擇器一個問題所困擾, 前的最後npm install它正常工作,現在我得到:錯誤:日期選擇:不被識別爲一個日期對象由DateAdapter

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

     } 
    } 

回答

7

根據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) 
     }); 
+0

感謝您節省我的時間! –

相關問題