Javascript 如何在 angularjs 中修剪()一个字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30506300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I trim() a string in angularjs?
提问by Slartibartfast
Is there an angular specific way? If not, should I use the built in jquery to do it? If I should use the built in jquery how do I get to the trim() function without using $ (or is that necessary)?
有角度特定的方式吗?如果没有,我应该使用内置的 jquery 来做吗?如果我应该使用内置的 jquery,如何在不使用 $ 的情况下使用 trim() 函数(或者是否有必要)?
Edit - Yes I know about str.trim(). Sorry. I need this to work in IE 8
编辑 - 是的,我知道 str.trim()。对不起。我需要它在 IE 8 中工作
Edit - As far as this question being a duplicate, I am asking specifically how to do this in angular where the answer referenced explains how to do it in javascript, node and jquery. Is there a way to do it using the built in jquery in angular?
编辑 - 至于这个问题是重复的,我特别询问如何在 angular 中执行此操作,其中引用的答案解释了如何在 javascript、node 和 jquery 中执行此操作。有没有办法使用 angular 中的内置 jquery 来做到这一点?
Edit - Apparently the answer is "AngularJS doesn't do this"
编辑 - 显然答案是“AngularJS 不这样做”
回答by Zee
Why don't you simply use JavaScript's trim():
为什么不简单地使用 JavaScript 的trim():
str.trim() //Will work everywhere irrespective of any framework.
For compatibility with <IE9use:
为了与<IE9使用兼容:
if(typeof String.prototype.trim !== 'function') {
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g, '');
}
}
Found it Here
在这里找到
回答by Sergiu Paraschiv
If you need only displaythe trimmed value then I'd suggest against manipulating the original string and using a filterinstead.
如果您只需要显示修剪后的值,那么我建议不要操作原始字符串并改用过滤器。
app.filter('trim', function () {
return function(value) {
if(!angular.isString(value)) {
return value;
}
return value.replace(/^\s+|\s+$/g, ''); // you could use .trim, but it's not going to work in IE<9
};
});
And then
进而
<span>{{ foo | trim }}</span>
回答by Shubham Nigam
use trim() method of javascript after all angularjs is also a javascript framework and it is not necessary to put $ to apply trim()
使用javascript的trim()方法毕竟angularjs也是一个javascript框架,没有必要把$应用到trim()
for example
例如
var x="hello world";
x=x.trim()
回答by Sheyda Hosseini
I insert this code in my tag and it works correctly:
我在我的标签中插入了这段代码,它工作正常:
ng-show="!Contract.BuyerName.trim()" >
回答by YaManicKill
JS .trim() is supported in basically everthing, except IE 8 and below.
JS .trim() 基本上支持所有东西,除了 IE 8 及更低版本。
If you want it to work with that, then, you can use JQuery, but it'll need to be <2.0.0 (as they removed support for IE8 in the 2.x.x line).
如果您希望它使用它,那么您可以使用 JQuery,但它需要 <2.0.0(因为他们在 2.xx 行中删除了对 IE8 的支持)。
Your other option, if you care about IE7/8 (As you mention earlier), is to add trim yourself:
如果您关心 IE7/8(正如您之前提到的),您的另一个选择是自己添加修剪:
if(typeof String.prototype.trim !== 'function') {
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g, '');
}
}

