900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > AngularJS下使用Kendo的DatePicker控件日期有时不显示

AngularJS下使用Kendo的DatePicker控件日期有时不显示

时间:2021-11-28 09:02:04

相关推荐

AngularJS下使用Kendo的DatePicker控件日期有时不显示

在AngularJS下使用Kendo的DatePicker控件时,在k-ng-model绑定了日期对象,但是页面在显示时,有时控件显示空白,但是有时又正常,具有一定随机性,在stackoverflow中也没找到类似状况和解决方法,经过分析跟踪后,确认问题是DatePicker控件的问题,控件说明文档中所述ng-model和k-ng-model是有区别的:

The first is to demonstrate the difference betweenng-model="dateString"andk-ng-model="dateObject".dateStringis bound to the input field's contents as a string — so it gets the formatted string date, whiledateObjectis bound to the widget'svalue()which in the case ofDatePickerreturns a JSDateobject. As you can see, we can apply the Angulardatefilter 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。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。