2017-09-16 113 views
-1

我有一個日曆控件,我想爲它設置一些值,但是我在下面的行中得到以下錯誤,並且值也沒有設置。如何使用primeng日曆設置日曆?

this.data.dateIn = this.selecteddata.dateIn; 

<p-calendar [(ngModel)]="data.dateIn" [showIcon]="true" name="dateInCalendar" 
      required #dateInCalendar="ngModel"> 
</p-calendar> 

錯誤:

ERROR Error: Uncaught (in promise): TypeError: date.getMonth is not a function TypeError: date.getMonth is not a function at Calendar.webpackJsonp.../../../../primeng/components/calendar/calendar.js.Calendar.formatDate (calendar.js:649) at Calendar.webpackJsonp.../../../../primeng/components/calendar/calendar.js.Calendar.updateInputfield (calendar.js:252) at Calendar.webpackJsonp.../../../../primeng/components/calendar/calendar.js.Calendar.writeValue (calendar.js:595) at forms.es5.js:1779 at forms.es5.js:2965 at Array.forEach() at FormControl.webpackJsonp.../../../forms/@angular/forms.es5.js.FormControl.setValue (forms.es5.js:2965) at forms.es5.js:4367 at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke (zone.js:365) at Object.onInvoke (core.es5.js:3890) at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke (zone.js:364) at Zone.webpackJsonp.../../../../zone.js/dist/zone.js.Zone.run (zone.js:125) at zone.js:758 at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:398) at Object.onInvokeTask (core.es5.js:3881) at Calendar.webpackJsonp.../../../../primeng/components/calendar/calendar.js.Calendar.formatDate (calendar.js:649) at Calendar.webpackJsonp.../../../../primeng/components/calendar/calendar.js.Calendar.updateInputfield (calendar.js:252) at Calendar.webpackJsonp.../../../../primeng/components/calendar/calendar.js.Calendar.writeValue (calendar.js:595) at forms.es5.js:1779 at forms.es5.js:2965 at Array.forEach() at FormControl.webpackJsonp.../../../forms/@angular/forms.es5.js.FormControl.setValue (forms.es5.js:2965) at forms.es5.js:4367 at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke (zone.js:365) at Object.onInvoke (core.es5.js:3890) at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke (zone.js:364) at Zone.webpackJsonp.../../../../zone.js/dist/zone.js.Zone.run (zone.js:125) at zone.js:758 at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:398) at Object.onInvokeTask (core.es5.js:3881) at resolvePromise (zone.js:710) [angular] at :4200/polyfills.bundle.js:7469:17 [angular] at Object.onInvokeTask (core.es5.js:3881) [angular] at drainMicroTaskQueue (zone.js:591) [] at XMLHttpRequest.ZoneTask.invoke (zone.js:464) []

+0

帖子裏面有date.getmonth – Sajeetharan

+2

@Sajeetharan代碼似乎是從primeng https://github.com/primefaces/primeng/blob/master/src/app/components/calendar/calendar.ts#L1235 – yurzui

+0

我看你是一個.Net開發者。很酷的你是那個平臺上的PrimeFaces。不知道你可以在.Net上運行JSF或更通用的Java代碼... – Kukeltje

回答

0

解決問題,提供幫助別人的解決方案,只是轉換selecteddata.dateIn.toDate()

this.data.dateIn = this.selecteddata.dateIn.toDate(); 
1
TypeError: date.getMonth is not a function 

=>我認爲你正在使用的功能之日起獲得的月份。但日期無效或格式錯誤。這就是爲什麼你的格式功能不起作用。

有了我的想法,請使用moment.js首先包裝您的日期。例如:

let selectedDate = moment(dateString); 

如果你想獲得一個月,你只需使用:

let month = ("0"+(selectedEndDate.get("month")+1)).slice(-2); 

您的日期格式與P-日曆無效。你可以用它傳遞給日曆:

this.currentDate = moment(new Date()).format('YYYY-MM-DD');