Skip to content

Instantly share code, notes, and snippets.

@olegp
Created December 30, 2015 12:52
Show Gist options
  • Save olegp/bcf65142b80c7357122f to your computer and use it in GitHub Desktop.
Save olegp/bcf65142b80c7357122f to your computer and use it in GitHub Desktop.
input placeholder shim
if (!('placeholder' in document.createElement('input'))) {
angular.module('directives').directive('placeholder', ['$timeout', function($timeout) {
return function($scope, $element, $attrs) {
var hasFocus = false;
$element.bind('focus', function() {
hasFocus = true;
if ($element.hasClass('placeholder')) {
$timeout(function() {
$element.val('');
$element.removeClass('placeholder');
});
}
});
$element.bind('blur', function() {
hasFocus = false;
if (!$element.val()) {
$timeout(function() {
$element.val($attrs.placeholder);
$element.addClass('placeholder');
});
}
});
if ($attrs.ngModel) {
$scope.$watch($attrs.ngModel, function(value) {
if (!hasFocus) {
if (value) {
$element.removeClass('placeholder');
} else {
$element.val($attrs.placeholder);
$element.addClass('placeholder');
}
}
});
}
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment