Last active
April 29, 2016 02:12
-
-
Save symonny/9246314ad5fd530fe0670e5d2ca8c369 to your computer and use it in GitHub Desktop.
angular-textarea-autosize
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
.directive('autoSize', ['$timeout', function($timeout) { | |
return function (scope, element, attr, ctrl) { | |
var minHeight; | |
var $shadowtx = angular.element("<textarea class='" + attr['class'] + "'></textarea>"); | |
//add to element parent | |
angular.element(element.parent()[0]).append($shadowtx); | |
element.css({resize: "none"}).parent().css({ position: "relative" }); | |
var update = function (addtext) { | |
$shadowtx.val(element.val() + (addtext ? addtext: '')); | |
element.css('height', Math.max($shadowtx[0].scrollHeight, minHeight) + 'px'); | |
}; | |
element.bind('keydown', function (event) { | |
var keyc = event.keyCode || event.which; | |
if (keyc == 13) { | |
update("\n"); | |
} | |
}).bind('keyup', function (event) { | |
var keyc = event.keyCode || event.which; | |
if ((keyc == 46) || (keyc==8)) { //delete, backspace, not fired by scope.$watch | |
update(); | |
} | |
}); | |
$timeout(function(){ | |
minHeight = element[0].offsetHeight; | |
$shadowtx.css({ | |
position: 'absolute', | |
top: -10000, | |
left: -10000, | |
resize: 'none', | |
width: element.width(), | |
height: element.height() | |
}); | |
update(); | |
}, 0); | |
scope.$watch(attr['ngModel'], function(v){update();}); | |
} | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
based on https://gist.github.com/thomseddon/4703968
my adjusted version added below makes sure that there are no extra spaces added under the text
<textarea class="form-control word-wrap" auto-size placeholder="Set Location…" ng-model="item.address" ui-focus="isInTabFocus(listIndex, 1)" tabindex="{{getTabIndex(listIndex,1)}}"></textarea>