我需要<textarea>的自动调整大小(弹性)。我需要使用<textarea>的,不能使用<离子textarea>的其他问题。
我在下面:How to autoresize the ion-input field vertically while i am typing&https://stackblitz.com/edit/expandable-input
<离子textarea>的工作:
@Directive({ selector: 'ion-textarea[autosize]' })
但是,不为<textarea>的
@Directive({ selector: 'textarea[autosize]' })
这里是我的Stackblitz(自动调整不工作):https://stackblitz.com/edit/expandable-input-htjncw
控制台错误信息:“错误类型错误:无法读取属性未定义的‘风格’......”
码:
adjust():void {
const textArea = this.element.nativeElement.getElementsByTagName('textarea')[0];
textArea.style.overflow = 'hidden';
textArea.style.height = 'auto';
textArea.style.height = textArea.scrollHeight + 'px';
}
如果要添加的指令textarea
代替离子textarea的,您可以直接从nativeElement
可以访问HTML元素。你不会通过getElementsByTagName
进行访问
adjust():void {
const textArea = this.element.nativeElement;
textArea.style.overflow = 'hidden';
textArea.style.height = 'auto';
textArea.style.height = textArea.scrollHeight + 'px';
}