angular-datepicker
angular-datepicker copied to clipboard
Change datetime field and get the changed value
I have an input field with datetime picker, bad when I pick the datetime everything is ok, but when I change this vaue in input field(not in template) the data doesn't change.
I have the same problem..
I have found a way to fix this:
In Module.directive('dateTime' ...) link method: element.bind("change", function (e) { //Custom method console.log("Input changed"); var newDate = parseDate(e.srcElement.value, e.srcElement.attributes["format"].value); scope.$broadcast('set-user-typed-date', { date: newDate }); clear(); });
This code to parse any date format: function parseDate(input, format) { // http://karpcom.blogspot.dk/2011/02/constructing-javascript-date-object.html format = format || 'dd/MM/yyyy'; // somedefault format var parts = input.match(/(\d+)/g), i = 0, fmt = {}; // extract date-part indexes from the format format.replace(/(yyyy|dd|MM)/g, function (part) { fmt[part] = i++; }); return new Date(parts[fmt['yyyy']], parts[fmt['MM']] - 1, parts[fmt['dd']]); }
And this in Module.directive('datePicker' ...) link method: scope.$on('set-user-typed-date', function (event, args) {//Custom code scope.setDate(args.date); });
set the value with "new Date()" not a String
I have tried mortenholmgaard's method, but on the ngModel, only the view value is changed, the modelValue is still not changed :(
flyawaychen send a plunker with example code please.
@yelnar +1 http://plnkr.co/edit/JEe8jb?p=preview
When I insert/change date by input (using keyboard) ng-model value is not updated.
@rjurado01 i changed this: parseDate(e.srcElement.value, e.srcElement.attributes["format"].value); to this: parseDate(e.originalEvent.srcElement.value, e.originalEvent.srcElement.attributes["format"].value);
and finally i do this:
var format = "yyyy-MM-dd"; var newDate = parseDate(e.originalEvent.srcElement.value, format);
and i have to do this:
scope.$apply();
This is very annoying , when editing the fields. @mortenholmgaard thanks for fix.