这段代码呢是网上荡的一部风然后自己改进了一部风,由于项目需要textarea的字符要求动态显示,所以通过jquery 和 angular配合使用了一下也许还有不少大神有
更简便的方法。我本人想了个笨招直接用jq操作DOM来完成。希望能帮到别人。
<span class="help-block" id="textcount">还可以输入300字</span>
<textarea class="form-control" id="txt" ng-change="changed()" rows="3" ng-model="article.summary"></textarea>
$scope.changed = function () {
$scope.textcount=300-$scope.article.summary.length;
if($scope.textcount<=0){
$scope.textcount=0;
$("#textcount").css("color","red")
}else{
$("#textcount").css("color","#ccc")
}
$("#textcount").text("还可以输入"+$scope.textcount+"字");
if ($scope.article.summary.length > 300) {
$scope.article.summary = $scope.article.summary.substr(0, 300);
}
};