1.首先是在HTML中要有對(duì)應(yīng)的元素:
?<ion-textarea autosize></ion-textare>
2.就是核心。ts的實(shí)現(xiàn):
//首先是導(dǎo)入
import { ElementRef, HostListener }from '@angular/core';?
創(chuàng)建監(jiān)聽??
@HostListener('input', ['$event.target'])?
onInput(textArea:HTMLTextAreaElement):void {
this.adjust();
}
// ngOnInit():void {
//? setTimeout(() => this.adjust(), 0);
// }
//是實(shí)現(xiàn)的核心代碼
adjust():void {
const textArea =this.element.nativeElement.getElementsByTagName('textarea')[0];
? textArea.style.overflow ='hidden';
? textArea.style.height ='auto';
? textArea.style.height = textArea.scrollHeight +'px';
}