就我而言,我正在使用带角度的 daterangepicker。我的目的是观察存储日期范围值的模型中的任何更改,并将其保存以供稍后进行 AJAX 调用。我面临同样的问题,因为它会在日期更改时两次触发事件,即使它只是“今天”:一次是具有 startDate 和 endDate 属性的对象,另一次是字符串。
它可以作为一种优势加以利用。
$scope.$watch(
'rangeOfDate',
function (newValue) {
// Due to a known bug of open source library daterangepicker, the event is hit twice
//upon change. Once it is an object, and once it is a string. So, use appropriately.
var selectedDateRange = new Object();
if (typeof (newValue) == 'object') {
selectedDateRange.startDate = new Date(newValue.startDate).toLocaleDateString();
selectedDateRange.endDate = new Date(newValue.endDate).toLocaleDateString();
//Do as you wish with this custom object
}
else if (typeof (newValue) == 'string') {
alert("string");
}
},
false);