Based on this amazing repo you can create a custom directive to handle that for you.
Just like you can see in this working Stackblitz project, that directive would look like this:
import { ElementRef, HostListener, Directive, OnInit } from '@angular/core';
@Directive({
selector: 'ion-textarea[autosize]'
})
export class Autosize implements OnInit {
@HostListener('input', ['$event.target'])
onInput(textArea:HTMLTextAreaElement):void {
this.adjust();
}
constructor(public element:ElementRef) {
}
ngOnInit():void {
setTimeout(() => this.adjust(), 0);
}
adjust():void {
const textArea = this.element.nativeElement.getElementsByTagName('textarea')[0];
textArea.style.overflow = 'hidden';
textArea.style.height = 'auto';
textArea.style.height = textArea.scrollHeight + 'px';
}
}
And that's it! In order to see it in action, you can use it in any page like this:
Component
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
dummyText: string = `Type a longer text to see how this expands!`;
constructor(public navCtrl: NavController) { }
}
View
<ion-header>
<ion-navbar>
<ion-title>Autosizing Textarea</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<ion-item>
<!-- Use rows="1" to initialize it as a single line text-area -->
<ion-textarea rows="1" name="dummyText" [(ngModel)]="dummyText" autosize></ion-textarea>
</ion-item>
</ion-content>