Foreword
Everyone may have encountered the need to limit the input when inputting. This article introduces the method of limiting the number of words entered in a textarea through Angular.js. Friends in need can refer to the following examples.
The example code is as follows
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>AngularJS 简单应用程序--输入字数限制</title> <!-- @author:sm @email:sm0210@qq.com @desc:AngularJS 简单应用程序--输入字数限制 --> </head> <!-- AngularJS 应用程序 您已经学习了足够多关于 AngularJS 的知识,现在可以开始创建您的第一个 AngularJS 应用程序: --> <body > <div ng-app="myTodoApp" ng-controller="myTodoCtrl"> <h2>我的笔记</h2> <p><textarea ng-model="message" cols="40" rows="10" ng-readonly="ngreadonly" ></textarea></p> <p> <button ng-click="save()">保存</button> <button ng-click="clear()">清除</button> </p> <p>剩下字符数:<span ng-bind="left()"></span></p> </div> <!--引入angular--> <script src="js/angular.min.js"></script> <script language="javascript"> var app=angular.module("myTodoApp",[]); app.controller("myTodoCtrl",function($scope){ $scope.message=""; $scope.ngreadonly = false; $scope.left = function(){ if($scope.message.length>100){ $scope.ngreadonly = true; return 0; } return 100-$scope.message.length; } $scope.clear = function(){$scope.message="";$scope.ngreadonly = false;} $scope.save = function(){$scope.message=""} }); </script> </body> </html>