简体   繁体   English

角度日期选择器问题

[英]angular date picker issue

I am using ngbdatepicker for my form.我正在为我的表单使用ngbdatepicker I am trying to save date as a separately.我想单独保存日期。 I need to get date as bellow.我需要得到如下日期。

<input class="form-control ngbfield custom-form-control" required [ngClass]="{'is-invalid':f.touched && birthDay.invalid}" placeholder="yyyy-mm-dd" name="dob" [readonly]="true" #birthDay="ngModel" [(ngModel)]="signupModel.birthDay" ngbDatepicker #d1="ngbDatepicker"/>

date is passed successfully.日期成功通过。 I console logged below values.我控制台记录了以下值。

console.log(this.signupModel.birthDay.year);
console.log(this.signupModel.birthDay.day);
console.log(this.signupModel.birthDay.month);

above values printed correctly.以上值打印正确。 but my problem is in error occurred in the terminal.但我的问题是在终端中发生了错误。 Please check my screen shot.请检查我的屏幕截图。 在此处输入图片说明

How I rectify this?我如何纠正这个?

seems like your birthDay -variable is defined with the type "Date".好像你的birthDay变量是用“Date”类型定义的。 the original JS "Date" object did not have those properties!原来的JS“Date”对象没有这些属性!

ngbdatepicker uses a simple model:https://ng-bootstrap.github.io/#/components/datepicker/examples ngbdatepicker 使用一个简单的模型:https ://ng-bootstrap.github.io/#/components/datepicker/examples

Model: {
  "year": 2020,
  "month": 1,
  "day": 16
}

please show your type-definition of birthDay .请显示您对birthDay类型定义。

you should define it like this:你应该这样定义它:

signupModel: {
  /* your properties here */
  birthDay: { year: number, month: number, day: number }
  /* more properties here */
};

live-demo: https://stackblitz.com/edit/angular-so-4?file=app%2Fdatepicker-basic.ts现场演示: https : //stackblitz.com/edit/angular-so-4?file=app% 2Fdatepicker-basic.ts

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM