3

I am looking for the first index the passes through my ng-if condition which is not always $index === 0, and cant be solved using $first.

Here's my code sample:

<div class="ticket-event" ng-if="!item.hidden" ng-repeat="item in ctrl.event.items">   
   <div class="title item" ng-class="{'active': $index === $first}"></div>
</div>

I want to add a class in the first occurrence of item.

6 Answers 6

6

You don't have to check if $index is $first in:

<div class="title item" ng-class="{'active': $index === $first}">

Change it to:

<div class="title item" ng-class="{'active': $first}">
Sign up to request clarification or add additional context in comments.

1 Comment

But when an item index that passed through the 2nd line didn't start from 0, lets say 5, $first directive doesnt work
2

Why not do this via css?

Assuming "ticket -event" is a spelling mistake, and it should be "ticket-event", the css is straightforward:

.ticket-event div:first-child { // css here }

1 Comment

I tried this .my-root-class ul li:first-child, but did not work, so, I am looking for a way to do this with angularjs
2

Here is what you need to do:

<div class="title item" ng-class='{active:$first}'></div>

Comments

0
<tr ng-class="{evaluationRow : $index==0 && item.HighlightBorder==true }" ng-repeat="item in items" >     
                <td>{{item.LearnerId}} </td>
                <td>{{item.Name}}</td>

This has worked for me. Here I'm setting Css class for first element in ng-repeat only if a condition holds true otherwise 'No' Css class is applied.

In above example, "evaluationRow" is the name of Css class to apply, followed by a condition.

Comments

0

You can make use of $first

It will evaluate to true for the first element in ng-repeat

So your code should be

<div class="ticket-event" ng-if="!item.hidden" ng-repeat="item in ctrl.event.items">   
   <div class="title item {{$first?'active':'someotherclass'}}"></div>
</div>

Comments

-1

This worked for me

<div class="title item" ng-class='{active:$first}'></div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.