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 8446717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:59:45+00:00 2026-06-10T09:59:45+00:00

How do you do a ternary with AngularJS (in the templates)? It would be

  • 0

How do you do a ternary with AngularJS (in the templates)?

It would be nice to use some in html attributes (classes and style) instead of creating and calling a function of the controller.

  • 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-10T09:59:47+00:00Added an answer on June 10, 2026 at 9:59 am

    Update: Angular 1.1.5 added a ternary operator, so now we can simply write

    <li ng-class="$first ? 'firstRow' : 'nonFirstRow'">
    

    If you are using an earlier version of Angular, your two choices are:

    1. (condition && result_if_true || !condition && result_if_false)
    2. {true: 'result_if_true', false: 'result_if_false'}[condition]

    item 2. above creates an object with two properties. The array syntax is used to select either the property with name true or the property with name false, and return the associated value.

    E.g.,

    <li class="{{{true: 'myClass1 myClass2', false: ''}[$first]}}">...</li>
     or
    <li ng-class="{true: 'myClass1 myClass2', false: ''}[$first]">...</li>
    

    $first is set to true inside an ng-repeat for the first element, so the above would apply class ‘myClass1’ and ‘myClass2’ only the first time through the loop.

    With ng-class there is an easier way though: ng-class takes an expression that must evaluate to one of the following:

    1. a string of space-delimited class names
    2. an array of class names
    3. a map/object of class names to boolean values.

    An example of 1) was given above. Here is an example of 3, which I think reads much better:

     <li ng-class="{myClass: $first, anotherClass: $index == 2}">...</li>
    

    The first time through an ng-repeat loop, class myClass is added. The 3rd time through ($index starts at 0), class anotherClass is added.

    ng-style takes an expression that must evaluate to a map/object of CSS style names to CSS values. E.g.,

     <li ng-style="{true: {color: 'red'}, false: {}}[$first]">...</li>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to use ternary operator in razor (specifically on HTML attributes)? I
I have following ternary statement: $.history.init(function(url) { load(url == ? #some-page : url); });
Making a ternary logic table, and I would like to make my own function
Possible Duplicate: Ternary Expression with Interfaces as a Base Class Classes: interface ISms {
How would one shorten the following using ternary operators? if ((pos - maxPos) ==
I am trying to use the conditional (ternary) operator to assign the proper lambda
Possible Duplicate: Python Ternary Operator In some languages including Java, C/C++, C#, etc. you
How to use ternary operator with System::Boolean? This sample code always returns true: bool
Possible Duplicate: Python Ternary Operator If Python would support the (x ? a :
After reading To ternary or not to ternary? and Is this a reasonable use

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.