Created
November 20, 2013 13:03
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<input type="datetime-local" datetime-picker ng-model="datetime" /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}); | |
} | |
}; | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 ?