Reputation: 869
I'm new to Angular. I want to make textarea autoresizable, like in this case https://www.primefaces.org/primeng/#/inputtextarea. I tried different approaches, but nothing works on Angular 5.
This code shows my initial version of textarea.
<div class="form-group" contenteditable="true">
<label for="description">Description</label>
<textarea type="text" class="form-control" id="description" [(ngModel)]="dish.description" #description="ngModel" name="description" required></textarea>
<div [hidden]="description.valid || description.pristine" class="alert alert-danger">Description can't be empty</div>
</div>
I'll appreciate your help, thanks.
Upvotes: 0
Views: 3738
Reputation: 3171
You may not need to write your own directive or event handler. You can use cdkTextareaAutosize
from the official Angular CDK.
Upvotes: 0
Reputation: 869
Finally I got solution
autogrow() {
let textArea = document.getElementById("description")
textArea.style.overflow = 'hidden';
textArea.style.height = 'auto';
textArea.style.height = textArea.scrollHeight + 'px';
}
In HTML:
<textarea style="resize:vertical" (keyup)="autogrow()" rows="3" (click)="autogrow()" type="text" class="form-control" id="description" [(ngModel)]="myprivateObject.description" #description="ngModel" name="description" required></textarea>
Upvotes: 2