Rob*_*tro 5 typescript primeng angular
我收到错误
core.es5.js:1020 错误错误:未捕获(承诺):类型错误:date.getMonth 不是函数
类型错误:date.getMonth 不是函数
每当我从后端接收 JSON 对象时尝试使用 2 路数据绑定时,都会收到此错误。我的后端数据类型是日期,但它不会在 p-calendar 输入中显示备份。请帮忙!
{ "applicationId": 1, "entryDate": 1524665731000, "ongoingDescription": "<p>Ongoing entry should end in (5081421)</p>", "activeFlag": "Y" }
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
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() {
}
Run Code Online (Sandbox Code Playgroud)
任何信息都会有所帮助
我也尝试分配 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) });
}
Run Code Online (Sandbox Code Playgroud)
您的变量entryDate永远不会被分配或使用。您可以删除它,除非您想引用 p-calendar 组件并可以访问它的属性和方法。在这种情况下,您必须使用如下属性装饰它:
@ViewChild('entryDate')
entryDate: Calendar;
Run Code Online (Sandbox Code Playgroud)
请注意,类型是日历,而不是日期。这是一个primeng类型,应该从primeng导入
| 归档时间: |
|
| 查看次数: |
11114 次 |
| 最近记录: |