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.
The replace() method is one of the most commonly used techniques to remove the character from a string in javascript. The replace() method takes two parameters, the first of which is the character to be replaced and the second of which is the character to replace it with.
To remove a substring from a string, call the replace() method, passing it the substring and an empty string as parameters, e.g. str. replace("example", "") . The replace() method will return a new string, where the first occurrence of the supplied substring is removed.
So, if you want to remove the first character of String, just create a substring from the second character. Since index starts from zero in String, "text". substring(1) is equivalent to deleting the first character.
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}}
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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With