2017-09-16 116 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');