我正在尝试在我的AngularJS应用程序中实现一个简单的datepicker指令.在
UI Bootstrap Documentation之后我每次都会收到此错误:
Datepicker directive: “ng-model” value must be a Date object, a number
of milliseconds since 01.01.1970 or a string representing an RFC2822
or ISO 8601 date.
我找到了一些其他相关问题here和here,但它们都没有帮助.
我尝试访问$parent和$parent.$parent,如建议的here,它将消除错误,但它不会更新模型.
这是一个重新发现问题的Plunker:
http://plnkr.co/edit/CAUGqZnH77SbknmGTFiL?p=preview
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.1/angular.min.js"></script>
<script src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.13.0.js"></script>
</head>
<body ng-app="myApp" ng-controller="AppControl as appCtrl">
<label>{{dt | date:'fullDate'}}</label>
<input type="text"
class="form-control"
datepicker-popup="MMMM d, yyyy 'at' h:mm a"
ng-model="dt"
is-open="opened"
datepicker-options="dateOptions"
ng-required="true"
close-text="Close" />
<span class="input-group-btn">
<button type="button" class="btn btn-default" ng-click="open($event)">Pick</button>
</span>
<script>
angular.module("myApp", ['ui.bootstrap']).controller("AppControl", ["$scope", function($scope) {
$scope.opened = false;
$scope.dateOptions = {};
$scope.dt = new Date();
$scope.open = function($event) {
$event.preventDefault();
$event.stopPropagation();
$scope.opened = true;
};
}]);
</script>
</body>
</html>
任何帮助表示赞赏.
最佳答案 问题在于您在属性’datepicker-popup’中提供的格式.
将属性的值用作’longDate’,如下所示:
datepicker-popup="longDate"
使用方法如下:
<input type="text"
class="form-control"
datepicker-popup="fullDate"
ng-model="dt"
is-open="opened"
datepicker-options="dateOptions"
ng-required="true"
close-text="Close" />
我更新了plnkr.