Skip to content

Instantly share code, notes, and snippets.

@HasAndries
Forked from oborder/gist:3103533
Created July 18, 2012 08:55
Show Gist options
  • Save HasAndries/3135128 to your computer and use it in GitHub Desktop.
Save HasAndries/3135128 to your computer and use it in GitHub Desktop.
angularjs directive for bootstrap datepicker : eternicode/bootstrap-datepicker, eyecon.ro
angular.module('bDatepicker', []).
directive('bDatepicker', function(){
return {
require: '?ngModel',
restrict: 'A',
link: function($scope, element, attrs, controller) {
var updateModel;
updateModel = function(ev) {
element.datepicker('hide');
element.blur();
return $scope.$apply(function() {
return controller.$setViewValue(ev.date);
});
};
if (controller != null) {
controller.$render = function() {
element.datepicker().data().datepicker.date = controller.$viewValue;
element.datepicker('setValue');
element.datepicker('update');
return controller.$viewValue;
};
}
return attrs.$observe('bDatepicker', function(value) {
var options;
options = {};
if (angular.isObject(value)) {
options = value;
}
if (typeof(value) === "string" && value.length > 0) {
options = angular.fromJson(value);
}
return element.datepicker(options).on('changeDate', updateModel);
});
}
};
});
@HasAndries
Copy link
Author

Use like:

<input b-datepicker="{{dateOptions}}" ng-model="dateObject" >

$scope.dateOptions = {format: 'dd/mm/yyyy'}

This works with angular v1.0.0 and bootstrap v2.0.4
The Datepicker is here: http://www.eyecon.ro/bootstrap-datepicker/ or https://github.com/eternicode/bootstrap-datepicker/
Thanks to Novi:
https://gist.github.com/3103533

@ejain
Copy link

ejain commented Sep 25, 2012

This works great, but I noticed that when I manually enter an invalid date and lose focus, the date is not corrected, and next time I focus the input, the browser can hang!

@danbarua
Copy link

@ejain https://gist.github.com/danbarua/5356062 <-- handles user entering invalid dates manually

@ronbuchanan
Copy link

This has been working quite well, except for a few things.

  1. When clicking on the input and then off, a date will still be put in the field rather than leaving it blank.
  2. How to set it to an initial date value? I tried "{ date: '1/1/2013' }" but it would not show it on the view.

@bluee
Copy link

bluee commented Sep 9, 2014

+1 Would be really nice if options were working as set here: b-datepicker="{{dateOptions}}"

@anilsingh581
Copy link

@danielbonnell
Copy link

@bluee did you ever have any luck getting options to work?

@Yeswanth-JG
Copy link

Check My Fork GIST

@MrSpider
Copy link

Check my simplified fork using momentjs and ES6 modules https://gist.github.com/MrSpider/a9c00eb652e4ed5e7c8cff5759f7438a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment