Search code examples
angularjsng-class

How to give a specific class when title has certain length in ng-repeat?


I can't get this to work.

<div ng-repeat="title in titles">
   <h1>{{title.name}}}</h1>
</div>

What I want to do, is to get the length of the title. So let's say if the title has 10 letters, I want to give it a specific class.

I tried this:

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

(found it here, by the way: angular ng-class if-else expression).

But it didn't work, so clearly I am doing something wrong.


Solution

  • In this case, you don't need ternary operator. It could be the following:

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

    JSFiddle demo