Angularjs中的字数限制(bootstrap中的首页)

Angularjs中的字数限制(bootstrap中的首页),angularjs,Angularjs,我想在angular js中应用单词计数(例如在文本框“我很好”中的单词计数应该是3),并且我还想限制单词计数 请帮忙 提前谢谢我做了一个统计表,用空格分隔单词 HTML: <body ng-controller="myCtrl"> Write in here! <input type="text" ng-model="myModel"/> <button ng-click="countWords(myModel)">Click Me&l

我想在angular js中应用单词计数(例如在文本框“我很好”中的单词计数应该是3),并且我还想限制单词计数 请帮忙

提前谢谢

我做了一个统计表,用空格分隔单词

HTML

<body ng-controller="myCtrl">
   Write in here!
   <input type="text" ng-model="myModel"/>
     <button ng-click="countWords(myModel)">Click Me</button>
   <h1 ng-show="wordCount">You have {{wordCount}} words!</h1>
</body>
var myApp = angular.module("myApp",[]);

myApp.controller("myCtrl", function($scope){

  $scope.split = [];
  $scope.wordCount = 0;

  $scope.countWords = function(text){

    /*Split by Space*/
    $scope.split = text.split(/(\s+)/);

    /*Remove all the blank spaces*/
    for(var i=0; i<=$scope.split.length; i++){
      if($scope.split[i] === " "){
        $scope.split.splice(i, 1);
      }
    }

    /*Count Words*/
    $scope.wordCount = $scope.split.length;

  }
});
如果您需要查看输入框中包含的单词数量,只需附加一个
$watch
任务或在该字段上使用
ng change
,每次更改时,您都会检查其中包含的单词数量,然后您可以拒绝更多的单词进行写入


我希望我能帮上忙。

你可以在这里找到一些好答案。你尝试过吗?请发布一些代码。没有人会为您编写它。$scope.countOf=function(text,count){var s=text?text.split(//\s+/):0;//它在空格/tab/enter console.log(s.length);$scope.wc=s.length;if(s.length==count){alert('limit exceed');//text.substr(0,text.length);}返回s?s.length:“”;};我已经使用了这个,因为我想在文本块活字计数,但我不能限制它的意思是-我想代理不能在某些字计数后键入请帮助!我在这里,我也想在3个文本块中使用这个函数,并希望显示每个bl的字数;我用{{wc}}来显示当前的字数,但这三个文本框中的数字都是相同的
enter code here$scope.countOf = function(text,count) {
        var s = text ? text.split(/\s+/) : 0; // it splits the text on space/tab/enter
          console.log(s.length);
           $scope.wc=s.length;
          if(s.length === count + 1){


          //  alert('limit exceded');
        //  text.substr(0,text.length);
          }
          return s ? s.length : '';
enter code here

      };
enter code here$scope.countOf = function(text,count) {
        var s = text ? text.split(/\s+/) : 0; // it splits the text on space/tab/enter
          console.log(s.length);
           $scope.wc=s.length;
          if(s.length === count + 1){


          //  alert('limit exceded');
        //  text.substr(0,text.length);
          }
          return s ? s.length : '';
enter code here

      };