How to create a character counter for textarea ? (

2019-09-20 21:55发布

CODE:

    <div class="form-group">
      <label class="control-label" for="content">Content</label>
      <textarea name="content" data-ng-model="vm.article.content" id="content" class="form-control" cols="30" rows="10" placeholder="1400 characters long at least" required ng-minlength="1400" ng-maxlength="14000" highlight-on-error></textarea>
        <!--counter-->
      <br>
      <span class="form-help">{{1400-vm.article.content.length}}         
       Characters</span>
      <div ng-messages="vm.form.articleForms.content.$error" role="alert">
        <p class="help-block error-text" ng-message="required">Article title is required.</p>
      </div>
    </div>

SITUATION:

I am trying to display an error message on submit if word count is too low with ng-messages: the error message does not appear.

I am also trying to update in real-time a word count of the text area. It doesn't udate.

What have I done wrong ?


REFERENCE:

angularjs text area character counter


P.S.:

I am using mean.js

1条回答
趁早两清
2楼-- · 2019-09-20 22:49

Working fine :

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js"></script>
<body ng-app> 
    <div class="form-group">
      <label class="control-label" for="content">Content</label>
      <textarea name="content" data-ng-model="vm.article.content" id="content" class="form-control" cols="30" rows="10" placeholder="1400 characters long at least" required ng-maxlength="1400" highlight-on-error></textarea>
        <!--counter-->
      <br>
      <span class="form-help">{{1400-vm.article.content.length}}         
       Characters</span>
      <div ng-messages="vm.form.articleForms.content.$error" role="alert">
        <p class="help-block error-text" ng-message="required">Article title is required.</p>
      </div>
    </div>
    
</body>

查看更多
登录 后发表回答