String count in textarea Angularjs

Asked

Viewed 1,517 times

1

I need to create a way to show the amount of remaining characters in a textearea registration of a description for a particular profile.

That part is the entry of the data:

<textarea class="form-control" type="text" 
          name="description_validate" ng-model="group.description" 
          ng-minlength='{{valideDescricao.description_validate.minlength}}' 
          ng-maxlength="{{valideDescricao.description_validate.maxlength}}" 
          ng-required='valideDescricao.description_validate.required' />

That other part is the part of my data visualization:

<td align="justify" style="width: 500px"  data-title="'Descrição'" filter="{description: 'text'}" >{{row.description}}</td>

I want to know if there is any way to do the operation with Angular without I have to do a whole function in Javascript, if there is any directive to do this functionality, or a standard form in controller and in the view.

2 answers

1


There is yes, in your case would be:

<textarea class="form-control" type="text" 
          name="description_validate" ng-model="group.description" 
          ng-minlength='{{valideDescricao.description_validate.minlength}}' 
          ng-maxlength="{{valideDescricao.description_validate.maxlength}}" 
          ng-required='valideDescricao.description_validate.required' />

<span>{{valideDescricao.description_validate.maxlength - group.description.length}} restantes</span>

*if you are using Angularjs 1.1.1 or a newer version you need to add ng-trim="false" in the textarea, follows an example:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app>
    <textarea ng-model="txtarea" ng-trim="false" maxlength="200"></textarea>
    <span>{{200 - txtarea.length}} caracteres restantes</span>
</body>

  • Really very simple and functional, without having to create a whole function in the controller and bring it to the view, all the answers I thought were creating external functions for one simple thing.

1

There is no need for external components. However you need to expand the default behavior of ng-model, since it does not propagate the properties of the element, maxlength among them:

angular.module('myApp', [])
.directive('ngModel', function attributeNgModelDirective() {
  return {
    require: 'ngModel',
    link: function(scope, el, attrs, ctrl) {
      ctrl.attributes = attrs; //adiciona os atributos do elemento como uma
                               // propriedade do modelo
    }
  };
})
.controller('myController', function($scope){});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.js"></script>
<div ng-app="myApp">
  <form name="meuForm">
    <div ng-controller='myController'>
       <input name="entrada" id="entrada" ng-model="description" ng-minlength="2" ng-maxlength="200" />
       <br/>
       <span>Caracteres restantes: {{meuForm.entrada.attributes.maxlength - meuForm.entrada.$viewValue.length}}</span>
       <pre>{{meuForm.entrada | json }}</pre>

    </div>
  </form>
</div>

  • 1

    A question, why doesn’t he show "199" ? He jumps straight from 200 pro 198

  • @That’s because of the presence of ng-minlength, which invalidates the model if the value is less than 2.

  • Thank you very much, I don’t understand much about Angularjs yet

  • @Always a pleasure to complete! We are all learning. =)

  • @Odosendai I also noticed this, he jumped from 200 to 198, in the other example of Mathias he usually shows 200, 199, 198 ... I didn’t know that ng-minlength would invalidate the writing of the number 199 on the screen in this case

  • @Philipramkeerat is the value, being invalid (you can notice by the flag $valid: false), is not propagated to the model by angular. I changed the answer to include a dump of the object, where you can see the flags. Note that $viewValue has the correct value, but $modelValue is only completed if you pass the validation.

  • @Philipramkeerat I changed the answer so that the calculation occurs against the length of $viewValue, thus preserving the behavior expected by the user.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.