Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

datepicker inside a modal not working

I am trying to use ui-bootstrap components to make a datepicker inside a modal. The datepicker has to send back a date formatted as a unix timestamp.

  1. this is working fine if the datepicker is not inside a modal (= the timestamp updates when the date is selected): http://plnkr.co/edit/xQFmVgJojiq6aG9U8f4H?p=preview

  2. then, i put the directive inside a modal: http://plnkr.co/edit/9zHQQPGAcomT5Vha33j3?p=preview

here are the controllers :

.controller('MyCtrl', [ '$scope', '$modal', function ($scope, $modal) {
    $scope.open = function () {
        var modalInstance = $modal.open({
            templateUrl: 'tplModal.html',
            controller: 'MyModalCtrl'
        });
    };
}])
.controller('MyModalCtrl', [ '$scope', '$modalInstance', function ($scope, $modalInstance) {
    $scope.required= {};
    $scope.disabled = function(date, mode) {
        return ( mode === 'day' && ( date.getDay() === 0 || date.getDay() === 6 ) );
    };
    $scope.minDate = new Date();
    $scope.$watch('dt', function() { 
        if ($scope.dt) $scope.required.timestamp = Math.floor($scope.dt.getTime() / 1000); 
        console.log('timestamp: ', $scope.required.timestamp, '/ dt: ', $scope.dt);
    });
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
}]);

and the html template of the modal :

<div class="modal-body">
    <div ng-model="dt">
        <datepicker min="minDate" show-weeks="false"></datepciker>
    </div>
    <div>
        dt <span class="uneditable-input span2">{{dt | date:'dd.MM.yyyy' }}</span>
        dt <span class="uneditable-input span2">{{ dt }}</span>
        timestamp <span class="uneditable-input span2">{{ required.timestamp }}</span>
    </div>
</div>

In this 2nd version the timestamp doesn't update when the date is changed (it's like the $watch was not working).

Do you know how to make this work ?

like image 453
François Romain Avatar asked Oct 24 '13 17:10

François Romain


People also ask

Why my bootstrap DatePicker is not working?

Because you're defining the code in the head the body and its contents haven't been loaded yet; so the selector finds no elements to initialize datepicker. If you don't want to use document. ready functionality you could also move the script to the end of the body tag.

How do I get the DatePicker in pop up?

You can open the DatePicker popup on input focus by calling the show method in the input focus event.

Can DatePicker be null?

You just need to set property valueDefault={null} and the DatePicker will be blank.


1 Answers

You need to use the famous "dot" in the ng-model expression as $modal is creating a trasclusion scope for the window's content. In other words, there is a a scope created between your controller and modal's content.

Anyway, using the dot in the ng-model expression (which is the best practice) solves the problem for you:

<div ng-model="dt.value">
  <datepicker min="minDate" show-weeks="false"></datepciker>
</div>

and in JavaScript:

$scope.dt = {};
$scope.$watch('dt.value', function(newValue) { 
  if (newValue) $scope.required.timestamp = Math.floor(newValue.getTime() / 1000); 
  console.log('timestamp: ', $scope.required.timestamp, '/ dt: ', newValue);
});

Working plunk here: http://plnkr.co/edit/Adst8I8S0e0DLcVnhpLq?p=preview

like image 68
pkozlowski.opensource Avatar answered Dec 22 '22 05:12

pkozlowski.opensource