Skip to content

Instantly share code, notes, and snippets.

@lemming
Last active December 21, 2015 08:28
Show Gist options
  • Save lemming/6278002 to your computer and use it in GitHub Desktop.
Save lemming/6278002 to your computer and use it in GitHub Desktop.
'use strict';
angular.module('saSchedulerApp.directives')
.directive('pickadate', [function () {
return {
require: '?ngModel',
link: function (scope, elm, attrs, ngModel) {
var pickadateOptions = { format: 'dd.mm.yyyy' };
if (!ngModel) {
$(elm).pickadate(pickadateOptions);
return;
}
pickadateOptions.onSet = function(event) {
var date = null;
if (event.select) {
date = new Date(event.select).toISOString();
}
var phase = scope.$root.$$phase;
if (!~['$apply', '$digest'].indexOf(phase)) {
scope.$apply(function() {
ngModel.$setViewValue(date);
});
}
};
var $input = $(elm).pickadate(pickadateOptions),
picker = $input.pickadate('picker');
ngModel.$render = function() {
picker.set('select', new Date(ngModel.$viewValue));
}
}
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment