我收到错误
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/