Show characters count while typing in textarea Angular 5

10.6k Views Asked by At

I am trying to write a code to show characters count below the textarea field. I found many solutions for doing this but i don't know why its not working for me.

Here is the code what i am trying:

<div class="form-group border-0 describe mb-0">
  <label for="describewishInput" i18n="@@describewishLabel" class="text-capitalize">Describe Your Wish:</label>
    <span ngbTooltip="Please describe your wish here"
            placement="top" class="tt tt-bottom mt-1 mr-0"></span>
      <textarea type="text" (ngModel)="yourWish" id="yourWish" class="form-control mb-1" maxlength="255" i18n-placeholder="@@describeyourwish"></textarea>
      <span class="pull-right char"><em>{{ 255 - yourWish.length }} characters left</em></span>
</div>

Here is the error i am getting

SignupFormComponent.html:121 ERROR TypeError: Cannot read property 'length' of undefined
at Object.eval [as updateRenderer] (SignupFormComponent.html:122)
at Object.debugUpdateRenderer [as updateRenderer] (core.js:14735)
at checkAndUpdateView (core.js:13849)
at callViewAction (core.js:14195)
at execEmbeddedViewsAction (core.js:14153)
at checkAndUpdateView (core.js:13845)
at callViewAction (core.js:14195)
at execEmbeddedViewsAction (core.js:14153)
at checkAndUpdateView (core.js:13845)
at callViewAction (core.js:14195)

Can you help me with this. Its very frustrating as i am working on it from 1 day.

Thanks in Advance.

1

There are 1 best solutions below

6
On

You need to use two way data binding [(ngModel)] ,

<textarea type="text" [(ngModel)]="yourWish" id="yourWish" class="form-control mb-1" maxlength="255" i18n-placeholder="@@describeyourwish"></textarea>

STACKBLITZ DEMO