Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • How to use Ternary Operator in AngularJS Expressions?

    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 11.7k
    Comment on it


    Use of Ternary Operator in AngularJs is one of the great feature  which provides an alternative to conditions such as if, else and end if. 


    • Using this feature you can easily use JavaScript functionalities in your HTML static document.
    • Before you go through an example of use of ternary operator you must be aware about ternary operator.
    • A Ternary operator is a combination of three expressions and two operators, the ? (Question mark) and : (Colon), which separates the condition from the result.

    {{ Condition ? expressionOne : expressionTwo }}
    The above syntax is executed in the following way:
    If the condition is true, it  evaluates the first expression and it will become the result. Else, it will evaluate the second expression and that becomes the result.

    Now.Go through the example to understand the use of ternary operator in angularJs

    In this example i am using a ternary operator where it will check wheather the month is having odd or even days.

    <div ng-app
      { month:1, odd:31 },
      { month:2, even:28 },
      { month:3, odd:31 },
      { month:4, even:30 },
      { month:5, odd:31 }]">
      <!-- LOOP.-->
      <div ng-repeat="List in Monthtype">
        <div>{{ 'Month' + List.month }} : {{ List.odd > 30 ? 'odd -' + List.odd : 'even -' + List.even}} </div>

    Month1 : odd -31
    Month2 : even -28
    Month3 : odd -31
    Month4 : even -30
    Month5 : odd -31

 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: