Skip to content

Instantly share code, notes, and snippets.

@f5io
Created November 1, 2013 12:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save f5io/7264792 to your computer and use it in GitHub Desktop.
Save f5io/7264792 to your computer and use it in GitHub Desktop.
AngularJS placeholder shiv directive.
angular.module('namespace.directives', []).directive('placeholder', ['$timeout', function($timeout) {
var i = document.createElement('input'),
support = ('placeholder' in i);
if (support) return {};
return {
restrict: 'A',
link: function(scope, elm, attrs) {
if (attrs.type === 'password') {
return;
}
$timeout(function(){
elm.val(attrs.value || attrs.placeholder);
elm.bind('focus', function(){
if (elm.val() === attrs.placeholder) {
elm.val('');
}
});
elm.bind('blur', function(){
if (elm.val() === '') {
elm.val(attrs.placeholder);
}
});
});
}
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment