如何使用PrimeNG日历设置日历?

时间:2017-09-16 10:45:25

标签: angular primeng primeng-calendar

我有一个日历控件,我想为它设置一些值,但我在下面的行中得到以下错误,值也没有设置。

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

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

错误错误:未捕获(在承诺中):TypeError:date.getMonth不是a   功能       TypeError:date.getMonth不是函数           在Calendar.webpackJsonp ... / .. / .. / .. / primeng / components / calendar / calendar.js.Calendar.formatDate   (calendar.js:649)           在Calendar.webpackJsonp ... / .. / .. / .. / primeng / components / calendar / calendar.js.Calendar.updateInputfield   (calendar.js:252)           在Calendar.webpackJsonp ... / .. / .. / .. / primeng / components / calendar / calendar.js.Calendar.writeValue   (calendar.js:595)           在forms.es5.js:1779           at forms.es5.js:2965           在Array.forEach()           在FormControl.webpackJsonp ... / .. / .. / forms/@angular/forms.es5.js.FormControl.setValue   (forms.es5.js:2965)           at forms.es5.js:4367           在ZoneDelegate.webpackJsonp ... / .. / .. / .. / zone.js / dist / zone.js.ZoneDelegate.invoke   (zone.js:365)           at Object.onInvoke(core.es5.js:3890)           在ZoneDelegate.webpackJsonp ... / .. / .. / .. / zone.js / dist / zone.js.ZoneDelegate.invoke   (zone.js:364)           在Zone.webpackJsonp ... / .. / .. / .. / zone.js / dist / zone.js.Zone.run(zone.js:125)           在zone.js:758           在ZoneDelegate.webpackJsonp ... / .. / .. / .. / zone.js / dist / zone.js.ZoneDelegate.invokeTask   (zone.js:398)           at Object.onInvokeTask(core.es5.js:3881)           在Calendar.webpackJsonp ... / .. / .. / .. / primeng / components / calendar / calendar.js.Calendar.formatDate   (calendar.js:649)           在Calendar.webpackJsonp ... / .. / .. / .. / primeng / components / calendar / calendar.js.Calendar.updateInputfield   (calendar.js:252)           在Calendar.webpackJsonp ... / .. / .. / .. / primeng / components / calendar / calendar.js.Calendar.writeValue   (calendar.js:595)           在forms.es5.js:1779           at forms.es5.js:2965           在Array.forEach()           在FormControl.webpackJsonp ... / .. / .. / forms/@angular/forms.es5.js.FormControl.setValue   (forms.es5.js:2965)           at forms.es5.js:4367           在ZoneDelegate.webpackJsonp ... / .. / .. / .. / zone.js / dist / zone.js.ZoneDelegate.invoke   (zone.js:365)           at Object.onInvoke(core.es5.js:3890)           在ZoneDelegate.webpackJsonp ... / .. / .. / .. / zone.js / dist / zone.js.ZoneDelegate.invoke   (zone.js:364)           在Zone.webpackJsonp ... / .. / .. / .. / zone.js / dist / zone.js.Zone.run(zone.js:125)           在zone.js:758           在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 [有角度]           at Object.onInvokeTask(core.es5.js:3881)[angular]           在drainMicroTaskQueue(zone.js:591)[]           在XMLHttpRequest.ZoneTask.invoke(zone.js:464)[]

2 个答案:

答案 0 :(得分:1)

TypeError: date.getMonth is not a function 

=&GT;我认为你正在使用该功能从日期开始计算月份。但是日期无效或格式错误。这就是为什么您的格式功能无法正常工作的原因。

根据我的想法,请先使用moment.js来包装你的日期。例如:

let selectedDate = moment(dateString);

如果你想获得月份,你只需使用:

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

您的格式日期对于p-calendar无效。您可以使用并将其传递给日历:

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

答案 1 :(得分:1)

解决了问题,提供了帮助他人的解决方案,只需将selecteddata.dateIn转换为.toDate()

this.data.dateIn = this.selecteddata.dateIn.toDate();