首页 > 解决方案 > 可以在 Angular 的 NG-Bootstrap Datepicker 中选择相同的日期

问题描述

我正在使用使用 ng-bootstrap 日期选择器的日期范围日期选择器。但是我遇到了一个问题。我应该可以选择相同的日期。我将如何解决这个问题?请检查此链接 点击这里

onDateSelection(date: NgbDate) {
    if (!this.fromDate && !this.toDate) {
      this.fromDate = date;
    } else if (this.fromDate && !this.toDate && date.after(this.fromDate)) {
      this.toDate = date;
    } else {
      this.toDate = null;
      this.fromDate = date;
    }
  }

标签: angularangular-reactive-formsng-bootstrapangular-forms

解决方案


删除第三个验证器else if

检查示例范围日期选择器

原来的

onDateSelection(date: NgbDate) {
    if (!this.fromDate && !this.toDate) {
      this.fromDate = date;
    } else if (this.fromDate && !this.toDate && date.after(this.fromDate)) {
      this.toDate = date;
    } else {
      this.toDate = null;
      this.fromDate = date;
    }
  }

固定一个

onDateSelection(date: NgbDate) {
    if (!this.fromDate && !this.toDate) {
      this.fromDate = date;
    } else if (this.fromDate && !this.toDate) {
      this.toDate = date;
    } else {
      this.toDate = null;
      this.fromDate = date;
    }
  }

推荐阅读