fengshihao
fengshihao

Reputation: 34

how to get the created event of element which generated by angularjs?

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?

Upvotes: 0

Views: 28

Answers (1)

dfsq
dfsq

Reputation: 193261

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

Upvotes: 1

Related Questions