简体   繁体   English

ion-textarea 在 Ionic 5 中动态调整高度

[英]ion-textarea resize height dynamically in Ionic 5

I am migrating my project from Ionc3 to Ionic5.我正在将我的项目从 Ionc3 迁移到 Ionic5。 I have an ion-textarea which increases the height as user types and it works in Ionic3.我有一个 ion-textarea 可以随着用户类型增加高度,它适用于 Ionic3。 Following is the code.以下是代码。

HTML page: HTML 页面:

<ion-textarea #hvUserPost type="text" (input)="adjustDesc()"></ion-textarea>

TS page: TS页面:

@ViewChild('hvUserPost') hvUserPost: ElementRef;
adjustDesc() {
    let textArea;
    textArea = this.hvUserPost['_elementRef'].nativeElement.getElementsByClassName("text-input")[0];
    textArea.style.overflow = 'hidden';
    textArea.style.height = 'auto';
    var scrollHeight = textArea.scrollHeight;
    textArea.style.height = scrollHeight + 'px';
}

Now in Ionic4 the only thing changes is in the following declaration现在在 Ionic4 中,唯一的变化是在下面的声明中

@ViewChild('hvUserPost', {static: false}) hvUserPost: ElementRef;

In Ionic4, I am getting the following error.在 Ionic4 中,我收到以下错误。

ERROR TypeError: Cannot read property 'nativeElement' of undefined

So this.hvUserPost['_elementRef'] is undefined and this.hvUserPost.nativeElement is also undefined.所以this.hvUserPost['_elementRef']是未定义的, this.hvUserPost.nativeElement也是未定义的。

Just add autoGrow="true" atteibute and it will be done.只需添加 autoGrow="true" atteibute 即可。

<ion-textarea #hvUserPost type="text"autoGrow="true" (input)="adjustDesc()"></ion-textarea>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM