Skip to content

Instantly share code, notes, and snippets.

@ichiban
Created November 20, 2013 13:03
Show Gist options
  • Save ichiban/7562794 to your computer and use it in GitHub Desktop.
Save ichiban/7562794 to your computer and use it in GitHub Desktop.
2-way bindings were broken with iOS Safari x input[type=datetime-local] x Angular.js. This directive fixed the problem for me.
<input type="datetime-local" datetime-picker ng-model="datetime" />
angular.module('app')
.directive('datetimePicker', [function() {
// input[type=datetime-local] in iOS Safari won't fire 'change' events as it changes,
// which breaks Angular's 2-way bindings.
// this directive fixes it by updating the model on 'blur' events.
return {
restrict: 'A',
priority: -1, // the priority of ngBlur is 0 (default) and this has to be prior to it.
require: 'ngModel',
scope: {},
link: function(scope, element, attrs, ngModel) {
if ('datetime-local' != attrs.type) { return; }
element.bind('blur', function() {
ngModel.$setViewValue(element.val());
scope.$apply();
});
}
};
}]);
@nvcken
Copy link

nvcken commented Jul 25, 2014

Hi , I use input type datetime-local & angularjs.
In the chrome datetime popup working fine, but on safari it has no button to call popup , it is only textbox.

Is this directives fix that problem ?

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