Javascript 从带有角度的字符串中删除字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30981591/
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
remove char from string with angular
提问by Ptheitroado
I'm trying to remove a period '.' from a value that comes from a feed, however I don't really want to do this in my app.js, rather in my view.
我正在尝试删除句号 '.' 来自来自提要的值,但是我真的不想在我的 app.js 中执行此操作,而是在我看来。
So if I do the following:
因此,如果我执行以下操作:
value: {{item.v_value}}
I get 3.5, I'd simply like to strip out and render out 35 instead.
我得到 3.5,我只是想去掉并渲染 35。
So basically reusing the replace function - but on the item value only.
所以基本上重用替换功能 - 但仅在项目值上。
回答by Cerbrus
Just use replace
:
只需使用replace
:
If v_value
is a string:
如果v_value
是字符串:
value: {{item.v_value.replace('.', '')}}
If v_value
is a number, "cast" it to a string first:
如果v_value
是数字,则首先将其“强制转换”为字符串:
value: {{(item.v_value + '').replace('.', '')}}
Basically, you can use JavaScript in those brackets.
基本上,您可以在这些括号中使用 JavaScript。
回答by area28
If you need it to be reusable you can use a filter.
如果您需要它可重复使用,您可以使用过滤器。
myApp.filter('removeString', function () {
return function (text) {
var str = text.replace('thestringtoremove', '');
return str;
};
});
Then in your HTML you can something like this:
然后在你的 HTML 中,你可以是这样的:
value: {{item.v_value | removeString}}