Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

175
Views
Angular - Adjust two way binded input element's height according to the length of its content

I want to make the height of my input text adjusted to its text content. I can easily achieve that when the user is filling the content by triggering the (input) event, which calls my adjustHeight function which updates the height of the input element using the scrollHeight.

Code example below:

 <textarea #myLabel
       (input)="adjustHeight(myLabel)"
       [(ngModel)]="labelValue">
 </textarea>


 adjustHeight(element: HTMLElement) {
   element.style.height = "5px";
   element.style.height = (element.scrollHeight)+"px";
 }

This works fine when the user is filling the input element, but when the input text is rendered with an already filled labelValue, (coming from an API response) the height of the textarea is not changed dynamically since no (input) event gets triggered.

I tried to use (ngModelChange) event but it does not seem to be triggered. Is there any other event that could be binded to the input element to take care of its height on rendering? Thanks in advance

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

template:

<textarea #myLabel
    [style.height]="getHeight(myLabel)" 
    [(ngModel)]="labelValue">
</textarea>

typescript:

public getHeight(element: HTMLElement): string {
    return element.scrollHeight + 'px';
}

this way, the style will change whenever necessary.

about 4 years ago · Juan Pablo Isaza Report

0

Try starting by initializing labelValue to '' first (when you declare it), and then assign its value as you should be doing now.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!