Search code examples
angularjsangularjs-orderby

Get the resorted array from angularjs ng-repeat expression


Suppose I have the following angularjs expression

<tr ng-repeat="person in people | orderBy:age">

Suppose in my controller, I want to get the first 5 people in this newly sorted list. How can I do that?

I'm hoping there's some $scope variable that's injected that I can retrieve.

Otherwise, I suppose in my controller, I can apply the same sort and get the first five, but it seems inefficient to apply the sort twice. Anyone know how I would do it in javascript anyways?


Solution

  • You can use as which will return the filtered array as a scope variable:

    <tr ng-repeat="person in people | orderBy:age as filteredPeople">
    

    Note this wasn't available in early versions of angular