angular - PrimeNG <p-calendar> 错误 date.getMonth 不是函数

标签 angular typescript primeng

我收到错误

core.es5.js:1020 ERROR Error: Uncaught (in promise): TypeError: date.getMonth is not a function

TypeError: date.getMonth is not a function



每当我从后端接收 JSON 对象时尝试使用 2 路数据绑定(bind)时,都会收到此错误。我的后端数据类型是日期,但它不会在 p 日历输入中显示备份。请帮忙!
{ "applicationId": 1, "entryDate": 1524665731000, "ongoingDescription": "<p>Ongoing entry should end in (5081421)</p>", "activeFlag": "Y" }

HTML
<div class="form-group">
  <label>Date of the Ongoing Incident</label>
  <br>
  <p-calendar required [(ngModel)]="entry.entryDate" name="entryDate" #entryDate="ngModel" [showIcon]="true" [showTime]="true" dateFormat="mm/dd/y 'EST'" hourFormat="24"></p-calendar>
  <div class="alert alert-danger" *ngIf="entryDate.touched && !entryDate.valid" >The date and time of the incident are required</div>
</div>

客服
 entryDate: Date;

 entry = {
 }

constructor(private service: OngoingService, private route: ActivatedRoute, 
private router: Router, private location: Location) {

this.id = this.route.snapshot.paramMap.get('id');
if (this.id) this.service.get(this.id).take(1).subscribe(service => 
this.entry = service);

}

ngOnInit() {

}

ngAfterContentInit() {

}

ngAfterViewInit() {

}

任何信息都会有所帮助

我也尝试分配
entryDate = new Date();和 entryDate = new Date(this.entryDate);

最佳答案

它不能自动从 JSON 转换为 Date,您必须自己分配它。

this.id = this.route.snapshot.paramMap.get('id');
if (this.id) {
    this.service.get(this.id).take(1)
        .subscribe(service => this.entry = { entryDate: new Date(service.entryDate) });
}
你的变量entryDate从未分配或使用。您可以删除它,除非您想引用 p-calendar 组件并访问其属性和方法。在这种情况下,您必须使用以下属性来装饰它:
@ViewChild('entryDate')
entryDate: Calendar;
请注意,类型是日历,而不是日期。是primeng类型,应该从primeng导入

关于angular - PrimeNG <p-calendar> 错误 date.getMonth 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49632959/

相关文章:

html - Angular2 rc5和 Electron 错误-使用以下命令无法解析组件

angular - 为什么 @ViewChild() 装饰器在这个使用 PrimeNG FullCalendar 的 Angular 组件中不起作用?

angular - 将 javascript 生成的元素插入 primeng turbotable?

angular - 向 PrimeNG 表添加新行时分页不会刷新

typescript - Typescript 中带有 bool 值的可区分联合

javascript - 响应不可分配给类型 Promise<any> 属性最终丢失?

angular - 在Windows中的 Angular 项目中更改任何内容时Docker都不会重新编译

json - Angular 2 表单序列化为 JSON 格式

Angular Formio 自定义组件

javascript - 如何计算对象属性值并在动态按钮中显示单独的值并将单击事件分配给它们