20

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.

1
  • Angular expressions are not simply javascript. So you can't assume that everything that would work in a javascript execution context will also work as a angular expression. Commented Oct 7, 2014 at 9:46

7 Answers 7

54

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>
Sign up to request clarification or add additional context in comments.

1 Comment

hi Cétia, this works fine when I got the data & display on input field (at the time of edit profile). But by using this solution field is non-writable. Please suggest the solution with writable input field. Thanks.
30

If you simply need it in one or two places it may be easier to split and join:

$scope.boundString = 'this is a string with spaces'

with that you could do in your template:

<span>my string is: {{ boundString.split(' ').join('') }}</span>

and you would get:

my string is: thisisastringwithoutspaces

another approach that has been mentioned is the regex version ('g' is for global):

<span>my string is: {{ boundString.replace(/ /g, '') }}</span>

I guess the point is that you can do whatever you want to a string within an expression. These examples are bad convention with respect to Angular dirty-checking. In Angular, bound functions (string.replace, string.split) get evaluated differently opposed to a specified value (string, boolean) when bound to a template's expression. The result of a bound function must be evaluated before Angular knows whether or not to update the DOM. This can be costly over a large app. I would suggest using another variable to track the un-spaced value:

$scope.noSpaces = $scope.boundString.replace(/ /g, '');

HTML:

<span>{{ noSpaces }}</span>

This way, when a digest loop is triggered, Angular will check if noSpaces has changed as opposed to evaluating boundString.replace(/ /g, '').

What if you are ng-repeating? Good question.

for (var idx = 0, idx < $scope.boundIterable.length, i++) {
    $scope.boundIterable[i].noSpaces = $scope.boundIterable[i].boundString.replace(/ /g, '');
}

HTML:

<ul ng-repeat="iterable in boundIterable">
    <li>{{ iterable.noSpaces }}</li>
</ul>

Comments

10

The directive mentioned works pretty well. But if you want to remove spaces for smaller texts, you can use

.split(" ").join("")

This replaces the complete spaces unlike .replace(" ","") which replaces only the first space.

Comments

3

You can replace all spaces by blank by using replace():

.replace(" ","")

2 Comments

This seems to replace only the first space.
You were looking for .replace(/ /g, '')
2

How about {{ string.trim() }}?

Source

1 Comment

This will remove spaces from the start and from the end of the string.
2

You can do it by using replace():

{{mystring.replace(" ","")}}

that's it I hope so.

Comments

0
removeSpaces() {
  originalText ="hi! here i'm";
  removedSpacesText = originalText.split(" ").join("");
}

1 Comment

Please add some explanation, how you came to this result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.