0

I used ng-repeat to generate some html elements . like this:

<textarea ng-repeat="t in tips">{{t.content}}</textarea>

then i want to resize all of textarea's hight according to t.content. and textarea width is fixed ,what should i do?

fengshihao
  • 34
  • 1
  • 3
  • 1
    possible duplicate of [Calling a function when ng-repeat has finished](http://stackoverflow.com/questions/15207788/calling-a-function-when-ng-repeat-has-finished) – Josep Oct 12 '14 at 11:35
  • http://stackoverflow.com/questions/454202/creating-a-textarea-with-auto-resize might help. – seenukarthi Oct 12 '14 at 11:35
  • @Karthikeyan there is a jQuery plugin for this . http://www.jacklmoore.com/autosize/ – fengshihao Oct 12 '14 at 13:14

1 Answers1

1

You should write a directive for this. Maybe similar to this basic one:

app.directive('adjustHeight', function($timeout) {
    return {
        link: function(scope, element) {
            $timeout(function() {
                element[0].style.height = element[0].scrollHeight + 'px';
            });
        }
    };
});

and use it like this:

<textarea ng-repeat="t in tips" adjust-height>{{t.content}}</textarea>

It's very naive of course, but you can get the idea. Of course you can build something more sophisticated on top of it if you need for example dynamic resize.

Demo: http://plnkr.co/edit/UDCjfQpXjhA4ISioq28W?p=preview

dfsq
  • 191,768
  • 25
  • 236
  • 258