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.
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
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 ?
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.
You can open the DatePicker popup on input focus by calling the show method in the input focus event.
You just need to set property valueDefault={null} and the DatePicker will be blank.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With