日期未显示在angular-bootstrap datepicker中 [英] Date not showing up in angular-bootstrap datepicker

查看:76
本文介绍了日期未显示在angular-bootstrap datepicker中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张表,其中包含一些日期,并且我正尝试使用angular ui引导程序来获取日期选择器来对其进行编辑.

I have a table which conatians some dates and I am trying to use angular ui bootstrap to get a datepicker to edit them.

虽然日期选择器可以很好地弹出,并且允许我选择一个没有问题的日期,但是直到我从弹出窗口中选择了一个新日期之后,日期本身才出现在输入中.

While the datepicker pops up fine and allows me to select a date with no issue, the date itself does not appear in the input until after I have selected a new one from the popup.

可以使用以下代码重新创建:

Can be recreated with this code:

<!doctype html>
<html ng-app="ui.bootstrap.demo">

<head>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.js"></script>
  <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-2.4.0.js"></script>
  <script src="example.js"></script>
  <link href="//netdna.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>

<body>

  <style>
    .full button span {
      background-color: limegreen;
      border-radius: 32px;
      color: black;
    }

    .partially button span {
      background-color: orange;
      border-radius: 32px;
      color: black;
    }
  </style>
  <div ng-controller="DatepickerPopupDemoCtrl">
    <table>
      <tbody>
        <tr ng-repeat="event in eventFixtures track by $index">
          <td>
            <div class="row">
              <div class="col-md-6">
                <p class="input-group">
                  <input type="text" class="form-control" ng-model="event.date" uib-datepicker-popup="dd/M/yyyy" is-open="event.isOpen" ng-click="event.isOpen = true;" />
                </p>
              </div>
            </div>
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</body>

</html>

和这个:

angular.module('ui.bootstrap.demo', ['ui.bootstrap']);
angular.module('ui.bootstrap.demo').controller('DatepickerPopupDemoCtrl', function ($scope) {
  $scope.eventFixtures = [
    {name: "1", date: "1-1-01"},
    {name: "2", date: "2-2-02"},
    {name: "3", date: "3-3-03"},
    {name: "4", date: "4-4-04"},
    {name: "5", date: "5-5-05"}
  ];

});

推荐答案

弄清楚了,问题在于日期数据源是字符串而不是Date对象.当您选择一个新的数据对象时,它会被写入变量中,但是直到那之前它是一个字符串,因此不会.

Figured it out, the issue was the date data source is a string not a Date object. When you select a new data object it is written into the variable but until then it's a string and as such won't.

如果在eventFixtures声明之后编写此代码,它将转换日期并说明问题:

If you write this after the eventFixtures declaration, it will transform the dates and illustrate the issue:

   for(var i = 0; i < $scope.eventFixtures.length; i++) {
        $scope.eventFixtures[i].date = new Date($scope.eventFixtures[i].date);
    }

这篇关于日期未显示在angular-bootstrap datepicker中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆