Angularjs TEXTAREA remaining word count

Source: Internet
Author: User

The process of writing a page using the TextArea text field, suddenly remembered that he can also add the remaining word count of this function.

The first way of thinking:

Press down on the keyboard key to trigger a method count:

<textarea cols= "rows=" "maxlength=" "title=" can only enter 100 words "ng-model=" text "ng-change=" Tolcount () "></ Textarea>
<p> can also enter {{count}} characters </p>

Note: This is not necessary to use JS, said can be counted, but there is a great disadvantage, as long as the keyboard press, on the count, for example, you press the Delete button, he also counted; and if you are copying and pasting on textarea, there is no count.

The second idea: Calculate the remaining number of words according to the length of the input content:

HTML section:

<! DOCTYPE html>
<title> text field word limit </title>
<body ng-controller= "Testctrl" >
<textarea cols= "rows=" "maxlength=" "title=" can only enter 100 words "ng-model=" text "ng-change=" Tolcount () "></ Textarea>
<p> can also enter {{count}} characters </p>
</body>
<script type= "Text/javascript" src= "Jquery.js" ></script>
<script type= "Text/javascript" src= "Angular.min.js" ></script>
<script type= "Text/javascript" src= "Test.js" ></script>

JS section:

var app = Angular.module (' app ', []);

App.controller (' Testctrl ', function ($scope) {
$scope. Count = 100;

$scope. Tolcount = function () {
Console.log ($scope. text.length);
$scope. Count = 100-$scope. text.length;
};
});

Angularjs TEXTAREA remaining word count

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.