Javascript angular.js textarea字符计数以排除计数空格和换行符

Javascript angular.js textarea字符计数以排除计数空格和换行符,javascript,angularjs,html,Javascript,Angularjs,Html,My textarea字段需要计算字符数,以显示相对于定义的maxlength剩余的字符数。 但当用户输入空格/换行符或从其他位置粘贴文本时,计数出错 我需要知道angular是否有任何指令或方法支持排除空格/换行符计数 <textarea class="form-control" maxlength="{{desc}}" my-maxlength = "{{desc}}" maxlen="maxlen" type="text" ng-model="problem.DESCRIPTION

My textarea字段需要计算字符数,以显示相对于定义的maxlength剩余的字符数。 但当用户输入空格/换行符或从其他位置粘贴文本时,计数出错

我需要知道angular是否有任何指令或方法支持排除空格/换行符计数

<textarea class="form-control"  maxlength="{{desc}}" my-maxlength = "{{desc}}" maxlen="maxlen" type="text" ng-model="problem.DESCRIPTION" aria-describedby="qDesc" id="questionDescription" name="questionDescription" ng-required="caseType" lang-check></textarea>
<p ng-show="problem.DESCRIPTION.length > (desc-5)" class="text-danger pull-right" id="qDesc">{{ desc - problem.DESCRIPTION.length}} {{ resource["textcount.sublabel.maximumcharacters"] }</p>

{{{desc-problem.DESCRIPTION.length}}{{resource[“textcount.sublabel.maximumcharacters”]}


您可以制作一个过滤器来返回非空白字符的数量

yourModule.filter('charCount', function() {
    return function(text) {
        return (text.match(/\S/g) || []).length;
    };
})
并在模板中使用它

problem.DESCRIPTION | charCount

您可以制作一个过滤器来返回非空白字符的数量

yourModule.filter('charCount', function() {
    return function(text) {
        return (text.match(/\S/g) || []).length;
    };
})
并在模板中使用它

problem.DESCRIPTION | charCount
这是您需要的angularjs解决方案,请尝试使用

ng trim=“假”

文本区域中。以下是示例

<body ng-app>
   <textarea ng-model="test" ng-trim="false" maxlength="1500"></textarea>
   <span>{{1500 - test.length}} left</span>
</body>

{1500-test.length}左
这是您需要的angularjs解决方案,请尝试使用

ng trim=“假”

文本区域中。以下是示例

<body ng-app>
   <textarea ng-model="test" ng-trim="false" maxlength="1500"></textarea>
   <span>{{1500 - test.length}} left</span>
</body>

{1500-test.length}左

您不能只运行
问题。DESCRIPTION.length
ng show
{}
中,它不会运行,您必须创建一个函数并插入
ng show
{}

在控制器中,创建一个函数来删除字符串的空格并返回其长度

JS:

$scope.countLength = function(str){
    if (str==undefined){
        return 0;                    
    }
    else{
        return str.replace(/ /g,'', '').length;  
    }
}
<p ng-show="countLength(problem.DESCRIPTION) > (desc-5)" class="text-danger pull-right" 
id="qDesc">{{ desc - countLength(problem.DESCRIPTION)}} 
{{ resource["textcount.sublabel.maximumcharacters"] }</p>
HTML:

$scope.countLength = function(str){
    if (str==undefined){
        return 0;                    
    }
    else{
        return str.replace(/ /g,'', '').length;  
    }
}
<p ng-show="countLength(problem.DESCRIPTION) > (desc-5)" class="text-danger pull-right" 
id="qDesc">{{ desc - countLength(problem.DESCRIPTION)}} 
{{ resource["textcount.sublabel.maximumcharacters"] }</p>

{{desc-countLength(problem.DESCRIPTION)} {{resource[“textcount.sublabel.maximumcharacters”]}

请看下面的示例:
您不能只运行
问题。DESCRIPTION.length
ng show
{}
中,它不会运行,您必须创建一个函数并插入
ng show
{}

在控制器中,创建一个函数来删除字符串的空格并返回其长度

JS:

$scope.countLength = function(str){
    if (str==undefined){
        return 0;                    
    }
    else{
        return str.replace(/ /g,'', '').length;  
    }
}
<p ng-show="countLength(problem.DESCRIPTION) > (desc-5)" class="text-danger pull-right" 
id="qDesc">{{ desc - countLength(problem.DESCRIPTION)}} 
{{ resource["textcount.sublabel.maximumcharacters"] }</p>
HTML:

$scope.countLength = function(str){
    if (str==undefined){
        return 0;                    
    }
    else{
        return str.replace(/ /g,'', '').length;  
    }
}
<p ng-show="countLength(problem.DESCRIPTION) > (desc-5)" class="text-danger pull-right" 
id="qDesc">{{ desc - countLength(problem.DESCRIPTION)}} 
{{ resource["textcount.sublabel.maximumcharacters"] }</p>

{{desc-countLength(problem.DESCRIPTION)} {{resource[“textcount.sublabel.maximumcharacters”]}

请看下面的示例:

我看不出这将如何帮助OP“支持排除空间/新行计数”我看不出这将如何帮助OP“支持排除空间/新行计数”