简体   繁体   中英

angular date picker issue

I am using ngbdatepicker for my form. 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". the original JS "Date" object did not have those properties!

ngbdatepicker uses a simple model:https://ng-bootstrap.github.io/#/components/datepicker/examples

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

please show your type-definition of 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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