如何获取angularjs生成的元素的created事件

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

本文关键字:元素 created 事件 angularjs 何获取 获取      更新时间:2023-09-26

我使用ng repeat生成了一些html元素。像这样:

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

然后我想根据t.content调整所有textarea的高度。并且文本区域宽度是固定的,我该怎么办?

您应该为此编写一个指令。也许与这个基本的类似:

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

并像这样使用:

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

当然,这很天真,但你可以理解。当然,如果你需要动态调整大小,你可以在上面构建一些更复杂的东西。

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