Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I trim() a string in angularjs?

Tags:

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"

like image 812
Slartibartfast Avatar asked May 28 '15 12:05

Slartibartfast


People also ask

How do I trim in AngularJs?

AngularJs String Trim : we can use JavaScript trim() method to remove spaces from both end. Here in this post we are going to explain and example for how you can use this function to Angular String Trim with example. we can also use our online example to edit and run the code online and demo.

What trim () in string does?

Trim() Removes all leading and trailing white-space characters from the current string.

How do you trim the value of a string?

The trim() method in Java String is a built-in function that eliminates leading and trailing spaces. The Unicode value of space character is '\u0020'. The trim() method in java checks this Unicode value before and after the string, if it exists then removes the spaces and returns the omitted string.

How do you trim a string in JavaScript?

trim() The trim() method removes whitespace from both ends of a string and returns a new string, without modifying the original string. Whitespace in this context is all the whitespace characters (space, tab, no-break space, etc.) and all the line terminator characters (LF, CR, etc.).


1 Answers

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

like image 107
Zee Avatar answered Sep 18 '22 11:09

Zee