Skip to main content
3 of 4
edited tags
200_success
  • 144.2k
  • 22
  • 188
  • 473

Counting characters inside of a textarea using Angular

I have a textarea and need to calculate how many characters have been typed into it.

Both of the following solutions work correctly—which would be preferable, and why? Is there an even better way to implement it?

Option A:

<textarea 
   id="descriptionEducation" 
   name="description" 
   rows="5" 
   [(ngModel)]="fruit.description"></textarea>

<div class="form-row__description">
    You have written {{ fruit.description.length }} characters
</div>

Option B:

<textarea 
   id="descriptionEducation" 
   name="description" 
   rows="5" 
   [ngModel]="fruit.description" #descriptionFruit></textarea>

<div class="form-row__description">
    You have written {{ descriptionFruit.value.length }} characters
</div>
ivanasetiawan
  • 183
  • 1
  • 1
  • 4