Bootstrap 日期选择器拆分问题

Bootstrap datepicker split issue

我正在使用 bootstrap 日期选择器。 将值从日期选择器发送到服务器时,我的格式是 mm/dd/yyyy 例如:06/24/2015

当我从服务器(“06/24/2015”)取回值时,我试图用 06/24/2015 填充日期选择器输入。

为此,我正在尝试做这样的事情:

$scope.startDate = ($scope.startDate == null) ? null : new Date($scope.startDate);

现在 $scope.startDate 是 2015 年 6 月 24 日星期三 00:00:00 GMT-0500(中部标准时间)

然后我将其格式化以在日期选择器输入中显示日期,如下所示:

$scope.startDate = $filter('date')($scope.startDate,'MM/dd/yyyy');

现在的值为 06/24/2015

一切正常,但我看到一个脚本错误:

TypeError: Cannot read property 'split' of undefined
at createParser (ui-bootstrap-tpls-0.11.0.min.js:8)
    at parse (ui-bootstrap-tpls-0.11.0.min.js:8)
    at m (ui-bootstrap-tpls-0.11.0.min.js:8)
    at link.k.$render (ui-bootstrap-tpls-0.11.0.min.js:8)
    at Object.<anonymous> (angular.js:23295)
    at l.$digest (angular.js:14235)
    at l.$apply (angular.js:14506)
    at l (angular.js:9659)
    at S (angular.js:9849)
    at XMLHttpRequest.D.onload (angular.js:9790)

查了很多问题,大部分都说这是datepicker的bug,githubmaster上有,还没有发布。 我想确定我解析日期的方式是否正确?有没有更好的方法来处理这个问题并避免出现脚本错误?

通常情况下,服务器数据会在您的应用程序初始化后花费一段时间。与此同时,datepicker 正在尝试处理 ng-model 中的 null。

虽然您当然可以等待 bootstrap 的修复,但我认为您可以考虑在服务器数据可用之前初始化一个日期,例如今天的日期或任何合适的日期。

$scope.startDate = ($scope.startDate == null) ? new Date() : new Date($scope.startDate);

编辑: 我在 jsbin 中编写了一个简单的日期选择器,但似乎无法在控制台中得到您的错误。也许你可以更新它并重现?