如何删除AngularJS绑定中的所有字符串空间? [英] How can I remove all string spaces in AngularJS binding?
本文介绍了如何删除AngularJS绑定中的所有字符串空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试这样做:
<div id="{{mystring.replace(/[\s]/g, \'\')}}"></div>
,但不起作用. "mystring"是$scope
上的对象,其字符串类似于"my string is this",但要从视图中删除空格.
but its not working. "mystring" is an object on $scope
with string like "my string is this" with spaces I want to remove from the view.
推荐答案
只需创建一个专用过滤器:
Just create a dedicated filter :
angular.module('filters.stringUtils', [])
.filter('removeSpaces', [function() {
return function(string) {
if (!angular.isString(string)) {
return string;
}
return string.replace(/[\s]/g, '');
};
}])
并像这样调用它:
<div id="{{'hi there'| removeSpaces}}"></div>
这篇关于如何删除AngularJS绑定中的所有字符串空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文