Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9223549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:06:12+00:00 2026-06-18T04:06:12+00:00

I want to dynamically add a css class to an <li> element I am

  • 0

I want to dynamically add a css class to an <li> element I am looping over.
The loop is like this:

<li ng-repeat="todo in todos" ng-class="{{todo.priority}}">
  <a href="#/todos/{{todo.id}}">{{todo.title}}</a>
  <p>{{todo.description}}</p>
</li>

In my todo model, I have the property priority which can be “urgent”, “not-so-important” or “normal” and I just want to assign the class for each element.

I know I can do this for a boolean with something like ng-class="{'urgent': todo.urgent}"
But my variable is not a boolean, but has three values.
How would I do this?
Note also that I do not want to use ng-style="..." since my class will alter several visual things.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:06:13+00:00Added an answer on June 18, 2026 at 4:06 am

    You can simply assign a function as an expression and return proper class from there.
    Edit: there is also better solution for dynamic classes. Please see note below.

    Snippet from view:

    <div ng-class="appliedClass(myObj)">...</div>

    and in the controller:

    $scope.appliedClass = function(myObj) {
        if (myObj.someValue === "highPriority") {
            return "special-css-class";
        } else {
            return "default-class"; // Or even "", which won't add any additional classes to the element
        }
    }
    

    Better way of doing this

    I’ve recently learned about another approach. You pass in an object which has properties corresponding to the classes you operate on, and the values are expressions or boolean variables. A simple example:

    ng-class="{ active: user.id == activeId }"

    In this case active class will be added to the element as long as user.id matches activeId from the $scope object!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to dynamically add links to my pages something like this: foreach (Node
I'm using SlickGrid and want to add dynamically a css class to a row,
I keep getting this error in the $('#savetickets-list') line. I want to dynamically add
I have a <div> element that I want to dynamically add some HTML <a>
I am using JavaScript to dynamically add an element to the DOM. I want
I want to Create dynamically, custom Tooltips like this or this from code Behind,
I want to add an external JavaScript or CSS file in my test.html dynamically.
I want to iterate over a set of p elements and add a dynamically
i want to dynamically add a class to all rows of a table except
I want to dynamically add search box using Javascript to web-page like shown here

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.