AngularJS Tutorial - Combine orderBy and limitTo filters








The following code shows how to combine orderBy and limitTo filters.

Example


<!DOCTYPE html>
<html  ng-app>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js"></script>
</head><!--from  w  w  w .  j  a va2s  . co  m-->
<body>
  <div ng-app="">
  <div ng-init="friends = [{name:'John', age:25}, {name:'Mary', age:28}]">
    I have {{friends.length}} friends. They are:
    <ul>
      <li ng-repeat="friend in friends  | limitTo:1 | orderBy:'-name'">
        [{{$index + 1}}] {{friend.name}} who is {{friend.age}} years old.
      </li>
    </ul>
    <ul>
      <li ng-repeat="friend in friends | orderBy:'-name' | limitTo:1">
        [{{$index + 1}}] {{friend.name}} who is {{friend.age}} years old.
      </li>
    </ul>
  </div>
</div>
</body>
</html>

The code above is rendered as follows: