Angular Textarea 高度自动变化

很多前端开发的朋友可能都会遇到textarea 输入框的高度不能自动随着用户的输入变化的问题,今儿小生也遇到了, 并通过网络上的信息解决了这个问题,于是将解决方法贴上,以作备忘。

  directiveApp.directive('autoHeight', function(){
        function autoHeight(elem){
            elem.style.height = 'auto';
            elem.scrollTop = 0; //防抖动
            elem.style.height = elem.scrollHeight + 'px';
        }

        return {
            scope: {},
            link: function (scope, ele, attrs) {
                var oriEle = $(ele).get(0);
                $(oriEle).focus();
                $(oriEle).bind('keyup click', function(e) {
                    autoHeight($(this).get(0));
                });
                var timer = setInterval(function(){
                    if($(oriEle).val()) {
                        autoHeight(oriEle);
                        clearInterval(timer);
                    }
                }, 100);
            }
        };
    });
Html code:
  <textarea auto-height></textarea>

  

不才,望谅。