I'm looking to automatically adjust the height of my input text based on its content. I have a solution that works when the user is actively typing, triggering the (input)
event and calling my adjustHeight
function to update the input element's height using scrollHeight
.
Check out the 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 method works perfectly while users are adding text, but doesn't adapt the textarea height when pre-filled with content from an API response due to the absence of an (input) event trigger.
I attempted to utilize the (ngModelChange)
event but found it ineffective. Are there any other events I could bind to the input element for handling its height during rendering? Thank you in advance