0

I am checking the length of a title and setting a class, based on that length. I noticed though, that the length is also checking the empty spaces, so when you have a space, it would count as +1.

I want to get rid of that. So I want to exclude the count of empty spaces in the length. How can I do it, if it's possible?

<h1 ng-class="{'specific-class': title.name.length >= 10}">{{title.name}}</h1>
1

3 Answers 3

1

You could check this by replacing the spaces with empty strings:

title.name.replace(' ','').length >= 10

The full line would be:

<h1 ng-class="{'specific-class': title.name.replace(' ','').length >= 10}">{{title.name}}</h1>

Say if title.name was 'Hello World!', title.name.length would be 12, but title.name.replace(' ','').length is 11.

EDIT

Turns out you can't use slashes inside HTML or Angular will convert them to html-safe characters. I'd suggest therefore to separate the checker out into its own module. I've attached a snippet so you can see how it's done:

angular
  .module("app", [])
  .controller("test", function($scope) {
    // Example title object. Just load title objects as you would normally.
    $scope.title = {name: 'The World of McNamara'};

    // The actual magic happens here:
    $scope.checker = function(word) {
      return word.replace(/\s/g, '').length >= 10;
    }
  })
.specific-class {
  background-color: yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<!-- snippet checks whatever is passed to it as title.name -->
<div ng-app="app" ng-controller="test">
  <h1 ng-class="{'specific-class': checker(title.name)}">{{title.name}}</h1>
</div>
Sign up to request clarification or add additional context in comments.

14 Comments

Nope, your suggestion doesn't work... I tried the edited one, but it doesn't pass the classname (while mine does), even when the length is 5 or something... Both of your snippets don't work btw. No classname is passed.
Can you post an example title name?
For example: "The World of McNamara". This is clearly more than 5, so it should work, yet the classname isn't being passed.
No need to, because you fixed it! I will suggest an edit with the right answer that worked for me, so that others can use it too. Thanks mate! Upvoted and accepted the answer!
Thanks! Glad I could help.
|
0

You could do in multiple ways to remove empty spaces from the string like using split and then join.

title.name.split(' ').join('')

Or could be done by regex approach:

title.name.replace(/ /g, '')

It is up to you how you want to implement.

3 Comments

Can you give the full one, using my snippet?
@Siyah, you just have to put .length at the end. Like: <h1 ng-class="{'specific-class': title.name.replace(/ /g, '').length >= 10}">{{title.name}}</h1> or <h1 ng-class="{'specific-class': title.name.split(' ').join('').length >= 10}">{{title.name}}</h1>
Doesn't work. I get this: Error: [$parse:syntax] Syntax Error: Token '/' not a primary expression at column 41 of the expression
0

You would have to use .trim :

title.name.trim().length

Or you could do :

title.name.replace(" ", "").length

EDIT :

In your code it would be like so :

<h1 ng-class="{'specific-class': title.name.replace(' ', '').length >= 10}">{{title.name}}</h1>

Using ternary logic :

<h1 ng-class="(title.name.replace(' ', '').length >= 10)? 'specific-class' : 'another-class' ">{{title.name}}</h1>

3 Comments

.trim() only strips out whitespace at either end of a string, not from inside the string.
Can you give the example, implemented with my snippet?
Nope, also not working. For example, I have the title: "World of Root" and I have made the length >= 12. This would mean that the class SHOULDN't be given, but it IS given... so it doesn't work. World of Root has 11 letters... so it seems like the spaces aren't excluded at all.

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.