ng-bootstrap 日期选择器无法在 select 之前设置默认日期
ng-bootstrap datepicker unable to set default date before select
我正在使用 ng-bootstrap 日期选择器。
这是我的 html 日期选择器输入
<input type="text"
id="datepicker{{i}}"
class="form-control"
formControlName="dateJoined"
ngbDatepicker
#incorporatedDatePicker="ngbDatepicker"
(click)="incorporatedDatePicker.toggle()"
(dateSelect)="onDateSelection($event)"
readonly>
但是我无法在日期选择器本身 select 之前为此输入字段设置默认日期。
我在这里使用的是响应式表单,并且我已经将 NgbDateStruct Date 指定为表单值
const strDateTime = driver['JoinDate'];
const arr = strDateTime.split(' ');
const strDate = arr[0];
const dateArr = strDate.split('/');
const ngbDate: NgbDateStruct = {
year: dateArr[2],
month: dateArr[1],
day: dateArr[0],
}
this.addDriverForm.patchValue({
dateJoined: ngbDate
})
dateJoined
控件的值应该是下一个结构:
this.dateJoined = new FormControl({
year: 2020,
month: 5,
day: 16,
});
您需要验证所有的道具都是数字,而不是字符串。
const ngbDate: NgbDateStruct = {
year: parseInt(dateArr[2], 10),
month: parseInt(dateArr[1], 10),
day: parseInt(dateArr[0], 10),
}
我正在使用 ng-bootstrap 日期选择器。 这是我的 html 日期选择器输入
<input type="text"
id="datepicker{{i}}"
class="form-control"
formControlName="dateJoined"
ngbDatepicker
#incorporatedDatePicker="ngbDatepicker"
(click)="incorporatedDatePicker.toggle()"
(dateSelect)="onDateSelection($event)"
readonly>
但是我无法在日期选择器本身 select 之前为此输入字段设置默认日期。
我在这里使用的是响应式表单,并且我已经将 NgbDateStruct Date 指定为表单值
const strDateTime = driver['JoinDate'];
const arr = strDateTime.split(' ');
const strDate = arr[0];
const dateArr = strDate.split('/');
const ngbDate: NgbDateStruct = {
year: dateArr[2],
month: dateArr[1],
day: dateArr[0],
}
this.addDriverForm.patchValue({
dateJoined: ngbDate
})
dateJoined
控件的值应该是下一个结构:
this.dateJoined = new FormControl({
year: 2020,
month: 5,
day: 16,
});
您需要验证所有的道具都是数字,而不是字符串。
const ngbDate: NgbDateStruct = {
year: parseInt(dateArr[2], 10),
month: parseInt(dateArr[1], 10),
day: parseInt(dateArr[0], 10),
}