Date not showing up in angular-bootstrap datepicker

余生颓废 提交于 2020-01-05 05:58:25

问题


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>

and this:

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"}
  ];

});

回答1:


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.

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);
    }


来源:https://stackoverflow.com/questions/41515151/date-not-showing-up-in-angular-bootstrap-datepicker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!