在AngularJS下使用Kendo的DatePicker控件時,在k-ng-model綁定了日期對象,可是頁面在顯示時,有時控件顯示空白,可是有時又正常,具備必定隨機性,在stackoverflow中也沒找到相似情況和解決方法,通過分析跟蹤後,確認問題是DatePicker控件的問題,控件說明文檔中所述ng-model和k-ng-model是有區別的:app
ng-model="dateString"
and k-ng-model="dateObject"
. dateString
is bound to the input field's contents as a string — so it gets the formatted string date, while dateObject
is bound to the widget's value()
which in the case of DatePicker
returns a JS Date
object. As you can see, we can apply the Angular date
filter on it.ng-model綁定的是一個string類型的變量,k-ng-model則綁定一個對象變量,在二者都綁定的情形下,控件顯示的是ng-model綁定的變量,因此能夠把控件的ng-model和k-ng-model都賦值,這樣就能保證值正常又顯示正確。spa
HTML代碼:code
<input kendo-date-picker k-ng-model="datestart" ng-model="datestartString" />
JavaScript代碼:orm
$scope.datestart = new Date(); $scope.datestartString = $scope.datestart.getFullYear() + '-' + ($scope.datestart.getMonth() + 1) + '-' + $scope.datestart.getDate();
有一點要注意,js日期對象的getMonth返回的月份爲0到11,因此上面代碼中才加了1。對象