在AngularJS下使用Kendo的DatePicker控件时,在k-ng-model绑定了日期对象,但是页面在显示时,有时控件显示空白,但是有时又正常,具有一定随机性,在stackoverflow中也没找到类似状况和解决方法,经过分析跟踪后,确认问题是DatePicker控件的问题,控件说明文档中所述ng-model和k-ng-model是有区别的:
- The first is to demonstrate the difference between
ng-model="dateString"
andk-ng-model="dateObject"
.dateString
is bound to the input field‘s contents as a string — so it gets the formatted string date, whiledateObject
is bound to the widget‘svalue()
which in the case ofDatePicker
returns a JSDate
object. As you can see, we can apply the Angulardate
filter on it.
ng-model绑定的是一个string类型的变量,k-ng-model则绑定一个对象变量,在两者都绑定的情形下,控件显示的是ng-model绑定的变量,所以可以把控件的ng-model和k-ng-model都赋值,这样就能保证值正常又显示正确。
HTML代码:
<input kendo-date-picker k-ng-model="datestart" ng-model="datestartString" />
JavaScript代码:
$scope.datestart = new Date(); $scope.datestartString = $scope.datestart.getFullYear() + ‘-‘ + ($scope.datestart.getMonth() + 1) + ‘-‘ + $scope.datestart.getDate();
有一点要注意,js日期对象的getMonth返回的月份为0到11,所以上面代码中才加了1。
时间: 2024-10-06 23:13:06