如何更改 ngx-daterangepicker-material 的默认值?

how to change default value of ngx-daterangepicker-material?

我正在做项目,我发现 ngx-daterangepicker-material 有一些困难我想更改日历的默认值。它默认显示 Today ,我想将其更改为 Last 30 days (请参见下图)。我在收缩器中更改了 this.selectedDay 但它不起作用。 任何人都可以解决如何将 Today 标签更改为 Last 30 days 的问题! 这是我的代码

html 文件

 <div class="row">
        <div class="col s6">
        <input type="text"
        ngxDaterangepickerMd
        [(ngModel)]="selected"
        [showCustomRangeLabel]="true"
        [alwaysShowCalendars]="alwaysShowCalendars"
        [ranges]="ranges"
        [showClearButton]="true"
        [showCancel]="true"
        [linkedCalendars]="true"
        [isTooltipDate] = "isTooltipDate"
        [isInvalidDate] = "isInvalidDate"
        [locale]="{applyLabel: 'Done'}"
        (rangeClicked)="rangeClicked($event)"
        (datesUpdated)="datesUpdated($event)"
        [keepCalendarOpeningWithRange]="keepCalendarOpeningWithRange"
        [showRangeLabelOnInput]="showRangeLabelOnInput"
        class="form-control"
        placeholder="Select please..."/>
      </div>
      <div class="col s6">
        <br/>
        <strong>Model: </strong>
        <br/> {{selected | json  }}
        <br> 

ts 文件

export class CustomRangesComponent implements OnInit {
  selected: any;
  alwaysShowCalendars: boolean;
  showRangeLabelOnInput: boolean;
  keepCalendarOpeningWithRange: boolean;
  maxDate: dayjs.Dayjs;
  minDate: dayjs.Dayjs;
  invalidDates: dayjs.Dayjs[] = [];
  tooltips = [
    {date: dayjs(), text: 'Today is just unselectable'},
    {date:  dayjs().add(2, 'days'), text: 'Yeeeees!!!'}
  ];
  inlineDateTime;
  ranges: any = {
    Today: [dayjs(), dayjs()],
    Yesterday: [dayjs().subtract(1, 'days'), dayjs().subtract(1, 'days')],
    'Last 7 Days': [dayjs().subtract(6, 'days'), dayjs()],
    'Last 30 Days': [dayjs().subtract(29, 'days'), dayjs()],
    'This Month': [dayjs().startOf('month'), dayjs().endOf('month')],
    'Last Month': [
      dayjs()
        .subtract(1, 'month')
        .startOf('month'),
      dayjs()
        .subtract(1, 'month')
        .endOf('month')
    ],
    'Last 3 Month': [
      dayjs()
        .subtract(3, 'month')
        .startOf('month'),
      dayjs()
        .subtract(1, 'month')
        .endOf('month')
    ]
  };

  isInvalidDate = (m: dayjs.Dayjs) =>  {
    return this.invalidDates.some(d => d.isSame(m, 'day') );
  }
  isTooltipDate = (m: dayjs.Dayjs) =>  {
    const tooltip = this.tooltips.find(tt => tt.date.isSame(m, 'day'));
    if (tooltip) {
      return tooltip.text;
    } else {
      return false;
    }
  }

  constructor() {
    this.maxDate = dayjs().add(2,  'weeks');
    this.minDate = dayjs().subtract(3, 'days');

    this.alwaysShowCalendars = true;
    this.keepCalendarOpeningWithRange = true;
    this.showRangeLabelOnInput = true;
    this.selected = {
      startDate: dayjs().subtract(1, 'days').set('hours', 0).set('minutes', 0),
      endDate: dayjs().subtract(1, 'days').set('hours', 23).set('minutes', 59)
    };
  }
  rangeClicked(range) {
    console.log('[rangeClicked] range is : ', range);
  }
  datesUpdated(range) {
    console.log('[datesUpdated] range is : ', range);
  }

  ngOnInit() {}
  choosedDateTime(e) {
    this.inlineDateTime = e;
  }
} 

enter image description here

谁能帮我做一下!

我已经弄明白了 - 你必须在声明它时立即初始化 this.selected 然后 不要在更改开始时用新对象重新分配 this.selected和结束日期。仅更改对象属性。


//as component class attribute member, initialize when declaring
selected: any = { startDate: null, endDate: null };

// then you can change what you want in the constructor, ngOnInit() or wherever you want by accessing the object properties
//do not reassign  the object like this.selected = {} !
this.selected.startDate = moment().subtract(4, 'days').startOf('day');
this.selected.endDate = moment().subtract(1, 'days').startOf('day');

我正在使用 moment 但它应该也适用于您的 dayjs 库。