Last active
December 19, 2015 02:38
-
-
Save EpokK/5884029 to your computer and use it in GitHub Desktop.
Placeholder directive : ngPlaceholder
(https://twitter.com/ririlepanda)
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
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