最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - remove char from string with angular - Stack Overflow

programmeradmin2浏览0评论

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.

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.

So basically reusing the replace function - but on the item value only.

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.

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.

So basically reusing the replace function - but on the item value only.

Share Improve this question asked Jun 22, 2015 at 13:45 PoiroPoiro 1,0114 gold badges11 silver badges20 bronze badges 1
  • Is v_value a string or a number? – Cerbrus Commented Jun 22, 2015 at 13:46
Add a comment  | 

2 Answers 2

Reset to default 31

Just use replace:

If v_value is a string:

value: {{item.v_value.replace('.', '')}}

If v_value is a number, "cast" it to a string first:

value: {{(item.v_value + '').replace('.', '')}}

Basically, you can use JavaScript in those brackets.

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:

value: {{item.v_value | removeString}}
发布评论

评论列表(0)

  1. 暂无评论