Different class for the last element in ng-repeat

Angularjs

Angularjs Problem Overview


I am creating a list using ng-repeat something like this

  <div ng-repeat="file in files">
   {{file.name}}
   </div>

But for the last element alone I would like to have a class (<div class="last">test</div>) included to it. how can i achieve this using ng-repeat?

Angularjs Solutions


Solution 1 - Angularjs

You can use $last variable within ng-repeat directive. Take a look at doc.

You can do it like this:

 <div ng-repeat="file in files" ng-class="computeCssClass($last)">
 {{file.name}}
 </div>

Where computeCssClass is function of controller which takes sole argument and returns 'last' or null.

Or

  <div ng-repeat="file in files" ng-class="{'last':$last}">
  {{file.name}}
  </div>

Solution 2 - Angularjs

It's easier and cleaner to do it with CSS.

HTML:

<div ng-repeat="file in files" class="file">
  {{ file.name }}
</div>

CSS:

.file:last-of-type {
    color: #800;
}
  

The :last-of-type selector is currently supported by 98% of browsers

Solution 3 - Angularjs

To elaborate on Paul's answer, this is the controller logic that coincides with the template code.

// HTML
<div class="row" ng-repeat="thing in things">
  <div class="well" ng-class="isLast($last)">
      <p>Data-driven {{thing.name}}</p>
  </div>
</div>

// CSS
.last { /* Desired Styles */}

// Controller
$scope.isLast = function(check) {
    var cssClass = check ? 'last' : null;
    return cssClass;
};

Its also worth noting that you really should avoid this solution if possible. By nature CSS can handle this, making a JS-based solution is unnecessary and non-performant. Unfortunately if you need to support IE8> this solution won't work for you (see MDN support docs).

CSS-Only Solution

// Using the above example syntax
.row:last-of-type { /* Desired Style */ }

Solution 4 - Angularjs

<div ng-repeat="file in files" ng-class="!$last ? 'class-for-last' : 'other'">
   {{file.name}}
</div>

That works for me! Good luck!

Solution 5 - Angularjs

You could use limitTo filter with -1 for find the last element

Example :

<div ng-repeat="friend in friends | limitTo: -1">
    {{friend.name}}
</div>

Solution 6 - Angularjs

The answer given by Fabian Perez worked for me, with a little change

Edited html is here:

<div ng-repeat="file in files" ng-class="!$last ? 'other' : 'class-for-last'"> {{file.name}} </div>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRahulView Question on Stackoverflow
Solution 1 - AngularjsPaul BritView Answer on Stackoverflow
Solution 2 - AngularjsaidanView Answer on Stackoverflow
Solution 3 - AngularjsScott SwordView Answer on Stackoverflow
Solution 4 - AngularjsFabian PerezView Answer on Stackoverflow
Solution 5 - AngularjsAnouar khaldiView Answer on Stackoverflow
Solution 6 - AngularjsT. NaikView Answer on Stackoverflow