AngularJS filter to trim white space

Filter in AngularJS to trim white space.

1
2
3
4
5
6
7
8
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

1
<div>{{ string | trim }}</div>
Share