Created
March 18, 2015 00:50
-
-
Save darlanalves/8c5f0c422d51e8e55d96 to your computer and use it in GitHub Desktop.
Autogrow directive for textarea
This file contains 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
$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