ionic 3 textarea 自动调整大小(不是 ion-textarea)
ionic 3 textarea autosize (not ion-textarea)
我需要为
我正在关注: & https://stackblitz.com/edit/expandable-input
正在工作:
@Directive({ selector: 'ion-textarea[autosize]' })
但是,不适用于
@Directive({ selector: 'textarea[autosize]' })
这是我的 Stackblitz(自动调整大小不起作用):https://stackblitz.com/edit/expandable-input-htjncw
控制台错误:"ERROR TypeError: Cannot read property 'style' of undefined ..."
代码:
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
而不是 ion-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';
}
我需要为
我正在关注:
@Directive({ selector: 'ion-textarea[autosize]' })
但是,不适用于
@Directive({ selector: 'textarea[autosize]' })
这是我的 Stackblitz(自动调整大小不起作用):https://stackblitz.com/edit/expandable-input-htjncw
控制台错误:"ERROR TypeError: Cannot read property 'style' of undefined ..."
代码:
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
而不是 ion-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';
}