Can a combination of AngularJS filter
, order
, or limitTo
for ng-repeat
mimic the _.last
in UnderscoreJS?
Asked
Active
Viewed 5,155 times
6

user3335966
- 2,673
- 4
- 30
- 33

phteven
- 1,383
- 2
- 16
- 29
2 Answers
15
Gloopy's answer is correct, but just a note... If you want to use Underscore, you can:
myApp.run(function($rootScope) {
$rootScope._ = _;
});
<div ng-repeat="item in _.last(items)">

Andrew Joslin
- 43,033
- 21
- 100
- 75
12
If I'm understanding your question correctly I think this fiddle would do it.
For this data:
$scope.items = [{sort: 1, name: 'First'},
{sort: 2, name: 'Second'},
{sort: 3, name: 'Third'},
{sort: 4, name:'Last'}];
If you don't want to actually sort the array and just take the last two items of the array as is (like underscore's last) you can try a negative limit (this would show Third, Last):
<div ng-repeat="item in items | limitTo:-2">
Also note you can chain the filters together like this example sorting the data in reverse and taking 2 items (this would show Last, Third):
<div ng-repeat="item in items | orderBy:'sort':true | limitTo:2">

Gloopy
- 37,767
- 15
- 103
- 71