Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS 1.3 - Datepicker initial format is incorrect

I started using AngularJS 1.3 yesterday and one problem I have found is that the initial date in the Datepicker is not in the defined format. The format of the date is correct after choosing a date.

The initial format is correct with AngularJS 1.2.10 as can be seen in the example by commenting out the script tag.

How can I set the initial format to be correct?

Plunker example

HTML

<!doctype html>
<html ng-app="plunker">
  <head>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0-rc.0/angular.min.js"></script>
    <!-- <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.js"></script> -->
    <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.11.0.js"></script>
    <script src="example.js"></script>
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
  </head>
  <body>

  <br>
  <br>
  <div ng-controller="DatepickerDemoCtrl">
    <div class="row">
        <div class="col-md-6">
            <p class="input-group">
              <input type="text" class="form-control" datepicker-popup="{{format}}" ng-model="dt" is-open="opened" min-date="minDate" max-date="'2015-06-22'" datepicker-options="dateOptions" date-disabled="disabled(date, mode)" ng-required="true" close-text="Close" />
              <span class="input-group-btn">
                <button type="button" class="btn btn-default" ng-click="open($event)"><i class="glyphicon glyphicon-calendar"></i></button>
              </span>
            </p>
        </div>
    </div>
  </div>
  </body>
</html>

JS

var app = angular.module('plunker', ['ui.bootstrap']);
app.controller("DatepickerDemoCtrl", function ($scope) {
  $scope.format = 'dd-MMMM-yyyy';

  $scope.today = function() {
    $scope.dt = new Date();
  };
  $scope.today();

  $scope.open = function($event) {
    $event.preventDefault();
    $event.stopPropagation();

    $scope.opened = true;
  };
});
like image 771
camden_kid Avatar asked Sep 09 '14 10:09

camden_kid


2 Answers

Not sure exactly what changed in 1.3, but there is a bug report for this.

Here's a drop-in directive for the initial value that fixes the problem until the bug is fixed. And here is a demo Plunker.

angular.module('myApp').directive('datepickerPopup', function (dateFilter, datepickerPopupConfig) {
    return {
        restrict: 'A',
        priority: 1,
        require: 'ngModel',
        link: function(scope, element, attr, ngModel) {
            var dateFormat = attr.datepickerPopup || datepickerPopupConfig.datepickerPopup;
            ngModel.$formatters.push(function (value) {
                return dateFilter(value, dateFormat);
            });
        }
    };
});
like image 170
Dave Johnson Avatar answered Nov 06 '22 16:11

Dave Johnson


app.controller("DatepickerDemoCtrl", function ($scope,$filter) { .. // $filter added
$scope.today = function() {
    $scope.dt = $filter('date')(new Date(), $scope.format);
};
<input type="text" class="form-control" datepicker-popup="{{format}}" ng-model="dt" ... // {{format}} added

Please check with these three modifications.

like image 26
Kalhan.Toress Avatar answered Nov 06 '22 16:11

Kalhan.Toress