Skip to content

Instantly share code, notes, and snippets.

@EpokK
Last active December 19, 2015 02:38
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EpokK/5884029 to your computer and use it in GitHub Desktop.
Save EpokK/5884029 to your computer and use it in GitHub Desktop.
Placeholder directive : ngPlaceholder (https://twitter.com/ririlepanda)
app.directive('ngPlaceholder', function() {
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, element, attr, ctrl) {
var value;
var placehold = function () {
element.val(attr.placehold)
};
var unplacehold = function () {
element.val('');
};
scope.$watch(attr.ngModel, function (val) {
value = val || '';
});
element.bind('focus', function () {
if(value == '') unplacehold();
});
element.bind('blur', function () {
if (element.val() == '') placehold();
});
ctrl.$formatters.unshift(function (val) {
if (!val) {
placehold();
value = '';
return attr.placehold;
}
return val;
});
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment