Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse list in ng-repeat

So I have an array that I'm using ng-repeat to render out but I want to reverse this list. Previously I have used a filter which i got from this answer: angular ng-repeat in reverse

But now I get an error: "TypeError: Object # has no method 'slice' at Scope." I'm not sure if this is because of a new version of AngularJS or I'm missing something.

This is my filter code:

.filter('reverse', function () {
return function(items) {
    return items.slice().reverse();
};

This is the code from my view from inside the div:

data-ng-repeat='feed in feeds | reverse'
like image 615
ph3bell Avatar asked Jan 22 '14 15:01

ph3bell


People also ask

What can I use instead of NG-repeat?

But ng-repeat is not the right thing to use when you have large datasets as it involves heavy DOM manipulations. And you should consider using ng-repeat with pagination. You can consider using transclusion inside a custom directive, to achieve the behavior you are looking for without using ng-repeat.

What does ng-repeat do?

Definition and Usage The ng-repeat directive repeats a set of HTML, a given number of times. The set of HTML will be repeated once per item in a collection. The collection must be an array or an object. Note: Each instance of the repetition is given its own scope, which consist of the current item.

How do you reverse a list in TypeScript?

reverse() is an inbuilt TypeScript function which is used to reverses the element of an array. Syntax: array. reverse();


2 Answers

a solution which doesn't sort the array, but only copies and reverses it: ng-repeat="item in items | orderBy : '[]': true"

like image 82
ChenR Avatar answered Oct 07 '22 00:10

ChenR


Please use directly Java-script slice function with reverse like this:

data-ng-repeat="item in items.slice().reverse()"
like image 29
Manoj Patidar Avatar answered Oct 07 '22 01:10

Manoj Patidar