Abel D
Abel D

Reputation: 1080

angular counting 2 chars for newline in text area

I am counting characters in text area and showing remaining characters. I am also using ng-trim. But new line is occupying 2 chars and i am able to minus only one from length. how to work around this?

<textarea ng-model="desc" ng-init = "desc= ''" ng-trim="false" placeholder="Type your description here"  maxlength="1000" role="textarea"></textarea>
<p>Characters remaining: {{1000-desc.length}} </p>

Can I count newlines and minus along with length. If I can then how can I get the no of new lines here in html?

Upvotes: 0

Views: 587

Answers (1)

Max Koretskyi
Max Koretskyi

Reputation: 105547

If you want to remove all whitespaces, including newlines from the characters count, you can define a function on the controller to do that:

  $scope.desc = '';
  $scope.length = 0;
  $scope.$watch('desc', function(value) {
      $scope.length = value.replace(/\s/g, '').length;
  });

And then simply use it like that in html:

<textarea ng-model="desc" placeholder="Type your description here"  maxlength="1000" role="textarea"></textarea>
<p>Characters remaining: {{length}}</p>

See this plunker

Upvotes: 1

Related Questions