Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove all string spaces in AngularJS binding?

Tags:

I try to do this:

<div id="{{mystring.replace(/[\s]/g, \'\')}}"></div>

but its not working. "mystring" is an object on $scope with string like "my string is this" with spaces I want to remove from the view.

like image 976
Aviv M Avatar asked Oct 07 '14 09:10

Aviv M


People also ask

How do you get rid of the middle space in a string?

The replaceAll() method of the String class replaces each substring of this string that matches the given regular expression with the given replacement. You can remove white spaces from a string by replacing " " with "".

How do you remove spaces from a string in TypeScript?

Use the replace() method to remove all whitespace from a string in TypeScript, e.g. str. replace(/\s/g, '') . The replace method takes a regular expression and a replacement string as parameters. The method will return a new string with all whitespace removed.

What are bindings in AngularJS?

Data binding in AngularJS is the synchronization between the model and the view. When data in the model changes, the view reflects the change, and when data in the view changes, the model is updated as well.


1 Answers

Just create a dedicated filter :

angular.module('filters.stringUtils', [])

.filter('removeSpaces', [function() {
    return function(string) {
        if (!angular.isString(string)) {
            return string;
        }
        return string.replace(/[\s]/g, '');
    };
}])

and call it like :

<div id="{{'hi there'| removeSpaces}}"></div>
like image 66
Jscti Avatar answered Oct 06 '22 01:10

Jscti