首页 > 解决方案 > 在 angularJS 中调整 textarea 的大小(在 uib-tab 中)

问题描述

嗨,我在 AngularJS 中使用https://hassantariqblog.wordpress.com/2016/07/27/angularjs-textarea-auto-resize-directive/根据输入调整文本区域的大小。我正在使用如下模板(description.tpl.html):

 <textarea class="form-control" auto-resize placeholder="Please enter description here..." style="resize: none;min-height:100px;" ng-model="description" autofocus></textarea>  

我在 2 个地方显示文本区域。但是,当我在下面的选项卡中使用 textarea 时,它的大小不会更新。但是,当我开始输入文本区域时,它会更新大小。

 <uib-tab heading= DESCRIPTION >                      
     <div ng-include="'description.tpl.html'"></div>
 </uib-tab>

So I tried to add this code, when the tab is selected, how can I call the directive?

element.css({ 'height': 'auto', 'overflow-y': 'hidden' });
element.css('height', element[0].scrollHeight + 'px');

提前致谢!

标签: angularjsangularjs-directivetextarea

解决方案


我添加了一个布尔范围变量,并在选择选项卡时将其设置为 true。在指令中添加了以下代码:

     scope.$watch('descriptionTabSelected', function(descriptionTabSelected){
                    if(descriptionTabSelected){
                        element.css({ 'height': 'auto', 'overflow-y': 'hidden' });
                        $timeout(function () {
                            element.css('height', element[0].scrollHeight + 'px');
                        }, 100);
                    }
                });

推荐阅读