1

I have unordered list in which I want different different class for each li which will be dynamic.

my code is

$scope.className = ['clr1','clr2','clr3','clr4','clr5'];

My Html :

  <div>
        <ul>
         <li  ng-repeat="item in mainitem"  ng-style="{color: color[$index]}" >
              <span  ng-class="{className [$index]}">{{ item.name}}</span>  // At this line I ahve added dynamic class but its not working
          </li>
        </ul>
   </div>

This is not applied class as per classname array !!

Is there any issue with this code ? Thanks for helping !!

2
  • Why not use CSS class with :nth-child(n+1), etc. Commented Feb 1, 2018 at 7:36
  • Thanks !! can you pls update with code ? Commented Feb 1, 2018 at 7:40

2 Answers 2

2

you can use [] instead of {}

 <div>
        <ul>
         <li  ng-repeat="item in mainitem"  ng-style="{color: color[$index]}" >
              <span  ng-class="[className[$index]]">{{ item.name}}</span>  
          </li>
        </ul>
   </div>

for example of working see this codepen codepen

Sign up to request clarification or add additional context in comments.

7 Comments

@CodeWithCoffee i provide a link below
Its not adding class !!
$scope.className = ['clr1','clr2','clr3','clr4','clr5']; - but I don't see any of those classes declared in the CSS in your CodePen
Hey Thanks its seems working this way !! Thank you very much !!
Not provided in CSS but its getting the classes
|
0

As the code is posted in the question, you are missing an opening brace when evaluating the class name

<span  ng-class="{className [$index]}">{{ item.name}}</span>

should be

<span  ng-class="{{className [$index]}">{{ item.name}}</span>  

when it doesn't evaluate, you will end up with no class at all

2 Comments

Then there is a second problem (which I will look for). But the error which I pointed out still has to be corrected.
Yes I missed it !! Although I am using second one itself !! But its not adding class

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.