[英]Change date on change event in ngb-datepicler
I have this range selection popup from ngb-datepicker here .我在这里有来自 ngb-datepicker 的范围选择弹出窗口。 I got the initial date from the backend it's working fine.
我从后端得到了初始日期,它工作正常。 but when I m updating the date it didn't change in the input field.
但是当我更新日期时它在输入字段中没有改变。 how to fix this error?
如何解决此错误?
<div class="form-group">
<div class="input-group">
<input
#dpFromDate
class="form-control"
placeholder="From (YYYY-MM-DD)"
name="dpFromDate"
[(ngModel)]="splitRuleDetail.startDate"
[ngModelOptions]="{updateOn: 'blur'}"
(input)="fromDate = validateInput(fromDate, dpFromDate.value)"
/>
<div class="input-group-append">
<button
class="btn btn-outline-secondary calendar"
(click)="datepicker.toggle()"
type="button"
></button>
</div>
</div>
</div>
<div class="form-group ml-2">
<div class="input-group">
<input
#dpToDate
class="form-control"
placeholder="To (YYYY-MM-DD)"
name="dpToDate"
[(ngModel)]="splitRuleDetail.endDate"
[ngModelOptions]="{updateOn: 'blur'}"
(input)="toDate = validateInput(toDate, dpToDate.value)"
/>
<div class="input-group-append">
<button
class="btn btn-outline-secondary calendar"
(click)="datepicker.toggle()"
type="button"
></button>
</div>
</div>
</div>
I have added TS file this is same as given exmaple.but I think there is no problem is ts file.我添加了 TS 文件,这与给定的 exmaple 相同。但我认为 ts 文件没有问题。
onDateSelection(date: NgbDate) {
if (!this.fromDate && !this.toDate) {
this.fromDate = date;
} else if (this.fromDate && !this.toDate && date && date.after(this.fromDate)) {
this.toDate = date;
} else {
this.toDate = null;
this.fromDate = date;
}
}
isHovered(date: NgbDate) {
return this.fromDate && !this.toDate && this.hoveredDate && date.after(this.fromDate) && date.before(this.hoveredDate);
}
isInside(date: NgbDate) {
return this.toDate && date.after(this.fromDate) && date.before(this.toDate);
}
isRange(date: NgbDate) {
return date.equals(this.fromDate) || (this.toDate && date.equals(this.toDate)) || this.isInside(date) || this.isHovered(date);
}
validateInput(currentValue: NgbDate | null, input: string): NgbDate | null {
const parsed = this.formatter.parse(input);
return parsed && this.calendar.isValid(NgbDate.from(parsed)) ? NgbDate.from(parsed) : currentValue;
}
step to follow要遵循的步骤
(value)
instead of [(ngModel)]
(value)
而不是[(ngModel)]
[ngModelOptions]="{updateOn: 'blur'}"
[ngModelOptions]="{updateOn: 'blur'}"
stackblits solution stackblits 解决方案
onDateSelection(date: NgbDate) {
if (!this.fromDate && !this.toDate) {
this.fromDate = date;
} else if (this.fromDate && !this.toDate && date && date.after(this.fromDate)) {
this.toDate = date;
this.sub.close(); // close manually
} else {
this.toDate = null;
this.fromDate = date;
}
}
isHovered(date: NgbDate) {
return this.fromDate && !this.toDate && this.hoveredDate && date.after(this.fromDate) && date.before(this.hoveredDate);
}
isInside(date: NgbDate) {
return this.toDate && date.after(this.fromDate) && date.before(this.toDate);
}
isRange(date: NgbDate) {
return date.equals(this.fromDate) || (this.toDate && date.equals(this.toDate)) || this.isInside(date) || this.isHovered(date);
}
validateInput(currentValue: NgbDate | null, input: string): NgbDate | null {
const parsed = this.formatter.parse(input);
return parsed && this.calendar.isValid(NgbDate.from(parsed)) ? NgbDate.from(parsed) : currentValue;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.