batman batman - 15 days ago 5
AngularJS Question

AngularJS ngRepeat: how to differentiate even/odd elements?

I'm new to angular and trying to do the following:

<tr data-ng-repeat="element in awesomeThings">
<div ng-if="$index %2 == 0">
<td class="even">
<a href="#">
{{element}}
</a>
</td>
</div>
<div ng-if="$index %2 != 0">
<td class="odd">
<a href="#">
{{element}}
</a>
</td>
</div>
</tr>


for the above code, both
ng-if
is passing. Where I'm making mistake?

Answer

Try $even and $odd properties. Refer the documentation.

Like :

<tr data-ng-repeat="element in awesomeThings">
<div ng-if="$even">
    <td class="even">
        <a href="#">
            {{element}}
        </a>
    </td>
</div>
<div ng-if="$odd">
    <td class="odd">
        <a href="#">
            {{element}}
        </a>
    </td>
</div>
</tr>
Comments