Hii,
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.
Syntax:
{{ 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
Example:
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
ng-init="Monthtype=[
{ 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>
</div>
</div>
Output:
Month1 : odd -31
Month2 : even -28
Month3 : odd -31
Month4 : even -30
Month5 : odd -31
0 Comment(s)