How to know which is the first item in the ngRepeat loop?

Asked

Viewed 197 times

1

How to know which is the first item in a loop ngRepeat?

For example, I want to add a class in a div, when it’s the first in the loop.

Example:

<div ng-repeat="name in ['guilherme', 'wallace', 'bigown', 'rray']" ng-class="{'bold' : se_for_o_primeiro_do_loop}">
</div>
  • Another negative? Would you kindly point out what is the problem with my question?

1 answer

5


Use the variable $first. It is intended to indicate which is the first element in ng-repeat.

Behold:

<div ng-repeat="name in ['guilherme', 'wallace', 'bigown', 'rray']" ng-class="{'bold' : $first}"></div>

You can also use the $last to indicate the last item in ng-repeat.

  • For the latter would be $last?

  • 1

    Miguel, Raca! has the $middle too! I don’t believe it! Angular is very top!

  • Yes, it is very useful @Wallacemaxters ... It has a vast community. No wonder there are so many people using it, and it has a huge online support, and good documentation.. There’s no reason not to use it. It’s really a matter of taste

Browser other questions tagged

You are not signed in. Login or sign up in order to post.