【问题标题】:PrimeNG <p-calendar> error date.getMonth is not a functionPrimeNG <p-calendar> 错误 date.getMonth 不是函数
【发布时间】:2018-09-12 22:48:19
【问题描述】:

我收到了错误

core.es5.js:1020 ERROR 错误:未捕获(承诺中):TypeError:date.getMonth 不是函数

TypeError: date.getMonth 不是函数

当我从后端接收我的 JSON 对象时尝试使用 2 路数据绑定时,我会收到此错误。我的后端数据类型是日期,但它不会在 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>

TS

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

【问题讨论】:

  • 在哪里将属性 entryDate 添加到您的条目对象?

标签: angular typescript primeng


【解决方案1】:

它不能自动从JSON转换为日期,你必须自己分配它。

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导入

【讨论】:

  • 这是一个巨大的帮助。感谢您的解释!
  • 啊.. 我应该早点看到这篇文章。这正是我所需要的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-09
  • 1970-01-01
  • 1970-01-01
  • 2019-10-17
  • 2023-04-07
相关资源
最近更新 更多