Skip to content

Instantly share code, notes, and snippets.

@darlanalves
Created March 18, 2015 00:50
Show Gist options
  • Save darlanalves/8c5f0c422d51e8e55d96 to your computer and use it in GitHub Desktop.
Save darlanalves/8c5f0c422d51e8e55d96 to your computer and use it in GitHub Desktop.
Autogrow directive for textarea
$module.directive('autogrow', autogrowDirective);
/**
* @directive autogrow
*/
function autogrowDirective() {
return {
link: linker,
require: '?ngModel'
};
function linker($scope, $element, $attrs, ngModel) {
var min = $attrs.rows | 0 || 1;
var max = $attrs.maxRows | 0;
function getSize() {
return String(ngModel && ngModel.$viewValue || $element.val()).split('\n').length;
}
$element.on('keyup focus', function() {
var size = getSize();
if (size >= min && (!max || (max && size <= max))) {
$element.attr('rows', size);
}
});
$scope.$on('$destroy', function() {
$element.off('keyup focus');
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment