Skip to content

Instantly share code, notes, and snippets.

@powellmichael
Last active July 24, 2017 11:25
Show Gist options
  • Save powellmichael/1cf9626888c48c403d0df0389793957f to your computer and use it in GitHub Desktop.
Save powellmichael/1cf9626888c48c403d0df0389793957f to your computer and use it in GitHub Desktop.
NG Character count directive
https://stackoverflow.com/questions/20603107/angularjs-text-area-character-counter
.directive('charCount', ['$log', '$timeout', function($log, $timeout){
return {
restrict: 'A',
compile: function compile()
{
return {
post: function postLink(scope, iElement, iAttrs)
{
iElement.bind('keydown', function()
{
scope.$apply(function()
{
scope.numberOfCharacters = iElement.val().length;
});
});
iElement.bind('paste', function()
{
$timeout(function ()
{
scope.$apply(function()
{
scope.numberOfCharacters = iElement.val().length;
});
}, 200);
});
}
}
}
}
}])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment