in Angularjs, Javascript

How to render ng-repeat in reverse order

Many times you would like to render the array contents in reverse when using ng-repeat. You can do that with a simple single line code which uses the slice and reverse method.

<tr ng-repeat="subs in subjects.slice().reverse()">

</tr>

The disadvantage to this way of printing the data is that the original array contents get reversed taking up your systems precious computational resources. There is also a better way of achieving the same using the orderBy filter.

<tr ng-repeat="subs in subjects| orderBy:'-' ">
     
</tr>

Notice the use of orderBy with ‘-‘.

 

Write a Comment

Comment