Skip to content

Instantly share code, notes, and snippets.

@lordfriend
Last active December 23, 2015 15:39
Show Gist options
  • Save lordfriend/6657139 to your computer and use it in GitHub Desktop.
Save lordfriend/6657139 to your computer and use it in GitHub Desktop.
currently cannot read value from template attribute which is a controller define model, so change the defaultParams if you need a different init option, or you can provider a constant value to template attribute.
angular.module('myApp').controller('myController', function($scope) {
$scope.daterangeModel = {
start: moment().subtract('days', 5),
end: moment()
};
});
'use strict';
angular.module('ngDaterangepicker', [])
.constant('defaultParams', {
opens: 'right',
ranges: {
'Today': [moment(), moment()],
'Yesterday': [moment().subtract('days', 1), moment().subtract('days', 1)],
'Last 7 Days': [moment().subtract('days', 6), moment()],
'Last 30 Days': [moment().subtract('days', 29), moment()],
'This Month': [moment().startOf('month'), moment().endOf('month')],
'Last Month': [moment().subtract('month', 1).startOf('month'), moment().subtract('month', 1).endOf('month')]
},
startDate: moment(),
endDate: moment()
})
.directive('daterangepicker', function (defaultParams, $timeout) {
return {
restrict: 'A',
scope: false,
require: '?ngModel',
link: function postLink(scope, element, attrs, ngModel) {
if(!ngModel) {
console.log('ngModel is unavailable')
return;
}
var refElement = $(element);
// Model -> UI
ngModel.$render = function() {
//TBD
}
var params = $.extend(defaultParams, {
startDate: attrs.startDate,
endDate: attrs.endDate,
minDate: attrs.minDate,
maxDate: attrs.maxDate,
dateLimit: attrs.dateLimit,
showDropdowns: attrs.showDropdowns,
showWeekNumbers: attrs.showWeekNumbers,
timePicker: attrs.timePicker,
timePickerIncrement: attrs.timePickerIncrement,
timerPicker12Hour: attrs.timePicker12Hour,
ranges: attrs.ranges,
opens: attrs.opens,
buttonClasses: attrs.buttonClasses,
applyClass: attrs.applyClaass,
cancelClass: attrs.cancelClass,
format: attrs.format,
separator: attrs.separator,
locale: attrs.locale
});
refElement.daterangepicker(params, function(start, end){
console.log(start + '-' + end);
ngModel.$setViewValue({start:start, end:end});
scope.$digest();
});
}
};
});
<p ng-controller="myController">
<button class="btn" ng-Model="daterangeModel" daterangepicker opens="left">
<i class="icon-calendar icon-large"></i>
<span>{{daterangeModel.start}} - {{daterangeModel.end}}</span>
<b class="caret"></b>
</button>
</p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment