【发布时间】:2019-01-16 14:03:49
【问题描述】:
我有一个小问题,因为我想在 bootstrap-datepicker 的选项中使用我的函数。我有检查日期是否在日期数组中的功能:
isInArray(array, value) {
return !!array.find(item => {
return item.getTime() == value.getTime()
});
}
我想在这个选项中使用它 (https://bootstrap-datepicker.readthedocs.io/en/latest/options.html#beforeshowmonth),所以我把它放在我的选项中:
this.datepickerOptionsForMonths = {
minViewMode: 1,
format: "mm-yyyy",
startDate: this.dateFrom,
endDate: this.dateTo,
beforeShowMonth: function (date: Date) {
return this.isInArray(this.datepickerRange, date.getDate());
}
};
现在是问题,因为编译已完成,一切似乎都很好,但随后在控制台中出现错误:
this.isInArray is not a function
也许问题是我已经在 datepickerOptionsForMonths 所在的同一正文中使用了此函数(在 ngOnInit 中)。有人可以帮我吗?
【问题讨论】:
-
这条线
return item.getTime() == value.getTime()工作吗? item 只是一个可能没有 getTime 方法的变量? -
另请注意,您可以使用
Array.prototype.some而不是Array.prototype.find以避免需要强制转换为布尔值。
标签: javascript angular typescript bootstrap-datepicker