Created
November 28, 2016 17:39
-
-
Save maxt3r/2485356e91a1969bdb6cf54902e61165 to your computer and use it in GitHub Desktop.
Ionic 2 ion-textarea autoresize
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// An autoresize directive that works with ion-textarea in Ionic 2 | |
// Usage example: <ion-textarea autoresize [(ngModel)]="body"></ion-textarea> | |
// Based on https://www.npmjs.com/package/angular2-autosize | |
import { Directive, HostListener, ElementRef } from "@angular/core"; | |
@Directive({ | |
selector: "ion-textarea[autoresize]" // Attribute selector | |
}) | |
export class Autoresize { | |
@HostListener("input", ["$event.target"]) | |
onInput(textArea: HTMLTextAreaElement): void { | |
this.adjust(); | |
} | |
constructor(public element: ElementRef) { | |
} | |
ngOnInit(): void { | |
this.adjust(); | |
} | |
adjust(): void { | |
let ta = this.element.nativeElement.querySelector("textarea"); | |
ta.style.overflow = "hidden"; | |
ta.style.height = "auto"; | |
ta.style.height = ta.scrollHeight + "px"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@kinoli Thaank you sir its working fine in ionic 3