日期选择器无法正确更新日期对象

时间:2019-07-15 15:10:05

标签: javascript html angular typescript datetime

我正在使用在https://github.com/DanielYKPan/date-time-picker处找到的日期时间选择器来获取/更新日期选择器的“开始”和“结束”日期对象(请参见TypeScript代码上的selectedMoments)。但是,每当我选择一个新日期并触发函数dateTimePickerActivate()(请参见HTML代码上的(dateTimeChange)=)时,我都会遇到TypeError(请参见下面的错误1),并且新日期只能在隐藏的对象中找到属性_pendingValue(请参见下面的错误2),它不会更新日期对象的实际值。

如何解决这些问题以正确更新selectedMoments对象数组?

HTML:

<mat-form-field class="full-width">
  <input matInput placehold="Time Range (Optional):" [selectMode]="'range'" [owlDateTimeTrigger]="dateTimePicker" [owlDateTime]="dateTimePicker" [formControl]="selectedMoments" (dateTimeChange)="dateTimePickerActivate()">
  <owl-date-time showSecondsTimer="'true'" hour12Timer="'true'" #dateTimePicker></owl-date-time>
</mat-form-field>

TypeScript:

// Date & Time Stored Value
// Note: Month starts at 0, so ex: February would == 1
public selectedMoments = [new Date(2018, 1, 12, 10, 30), new Date(2018, 3, 21, 20, 30)];

dateTimePickerActivate() {
  console.log(Object.getOwnPropertyNames(this.selectedMoments));
  console.log(this.selectedMoments);
}

错误:

1)html ERROR TypeError: control.registerOnChange is not a function关于(dateTimeChange)="dateTimePickerActivate()"

2)

pic1 pic2

1 个答案:

答案 0 :(得分:0)

我找到了更新日期的解决方案(尽管有点古怪),但是我仍然没有找到解决第一个错误的方法。

HTML:

(dateTimeChange)="dateTimePicked(dateTimePicker)"

TypeScript:

dateTimePicked(selectedDate) {
  const dateRangeObject = {
    // First date
    from: selectedDate.selecteds[0],
    // Second date
    to: selectedDate.selecteds[1],
  };

  console.log(dateRangeObject);
  // do something with dates here...
}