22

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)?

Edit - Yes I know about str.trim(). Sorry. I need this to work in 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?

Edit - Apparently the answer is "AngularJS doesn't do this"

Slartibartfast
  • 1,605
  • 2
  • 16
  • 23
  • [trim()](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/trim) is a built in method on the String prototype, supported on most browsers. The MDN link provides a polyfill – CodingIntrigue May 28 '15 at 12:16
  • Your edit is very unfair to the people who have already answered. You should include that kind of stipulation at the offset. – CodingIntrigue May 28 '15 at 12:19
  • possible duplicate of [Trim string in JavaScript?](http://stackoverflow.com/questions/498970/trim-string-in-javascript) – Qantas 94 Heavy May 28 '15 at 12:19
  • Qantas 94 Heavy - yes the jquery example is there but I didn't know if that would work in angular since the $ is overridden. – Slartibartfast May 28 '15 at 12:21
  • I agree with @CodingIntrigue, your edits are unfair to others who have answered. also, while angular does not provide an out of the box solution IMHO a filter is the angular way of doing this. – alphapilgrim May 01 '18 at 21:04

5 Answers5

26

Why don't you simply use JavaScript's trim():

str.trim() //Will work everywhere irrespective of any framework.

For compatibility with <IE9 use:

if(typeof String.prototype.trim !== 'function') {
  String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, ''); 
  }
}

Found it Here

Community
  • 1
  • 1
Zee
  • 8,420
  • 5
  • 36
  • 58
  • 2
    Well...not everywhere...e.g. in – CodingIntrigue May 28 '15 at 12:17
  • http://kangax.github.io/compat-table/es5/#String.prototype.trim. Only not in Strict mode in IE9 – com2ghz May 28 '15 at 12:18
  • 1
    @Slartibartfast. Found a solution [here](http://stackoverflow.com/a/2308157/3894168). Also updated in code. – Zee May 28 '15 at 12:22
  • This works and I am aware of this solution, but I was after an angular specific way, possibly using the built in jquery. It appears this doesn't exist. – Slartibartfast May 28 '15 at 12:26
  • @Slartibartfast. There is no inbuilt method in angular for this. you can however use the other solution which will work regardless of browser versions. – Zee May 28 '15 at 12:28
20

If you need only display the trimmed value then I'd suggest against manipulating the original string and using a filter instead.

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>
Sergiu Paraschiv
  • 9,929
  • 5
  • 36
  • 47
13

use trim() method of javascript after all angularjs is also a javascript framework and it is not necessary to put $ to apply trim()

for example

var x="hello world";
x=x.trim()
Shubham Nigam
  • 3,844
  • 19
  • 32
  • Earlier in question it was not mention that it is not working for you in IE ,so that now you can use str.replace(/^\s+|\s+$/g, ' '); – Shubham Nigam May 28 '15 at 12:24
6

I insert this code in my tag and it works correctly:

ng-show="!Contract.BuyerName.trim()" >
A J
  • 3,970
  • 14
  • 38
  • 53
1

JS .trim() is supported in basically everthing, except IE 8 and below.

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).

Your other option, if you care about IE7/8 (As you mention earlier), is to add trim yourself:

if(typeof String.prototype.trim !== 'function') {
  String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, ''); 
  }
}
YaManicKill
  • 173
  • 6