Last active
February 8, 2021 11:26
-
-
Save enapupe/2a59589168f33ca405d0 to your computer and use it in GitHub Desktop.
Auto Grow/Shrink textarea directive for AngularJS (credits to this gist https://gist.github.com/thomseddon/4703968, specially @huyttq)
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
angular.module("myApp").directive("autoGrow", function(){ | |
return function(scope, element, attr){ | |
var update = function(){ | |
element.css("height", "auto"); | |
var height = element[0].scrollHeight; | |
if(height > 0){ | |
element.css("height", height + "px"); | |
} | |
}; | |
scope.$watch(attr.ngModel, function(){ | |
update(); | |
}); | |
attr.$set("ngTrim", "false"); | |
}; | |
}); |
in case textarea is not yet visible:
if(!element.is(':visible')) {
scope.$watch(function () { return element.is(':visible'); }, function (visible) {
if(visible) {
autogrow();
scope.$applyAsync();
}
});
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@lpsBetty
Your trick of binding
input
event is great! Thanks.PS:
I was implementing
autogrowing
textarea inionic 1
modal template while I should consider a delay effect of modal opening. So I added amodal.shown
watcher.Hope this might help someone.