Improve this Doc  View Source

limitTo

  1. - filter in module ng

Creates a new array or string containing only a specified number of elements. The elements are taken from either the beginning or the end of the source array, string or number, as specified by the value and sign (positive or negative) of limit. If a number is used as input, it is converted to a string.

Usage

In HTML Template Binding

{{ limitTo_expression | limitTo : limit : begin}}

In JavaScript

$filter('limitTo')(input, limit, begin)

Arguments

Param Type Details
input Arraystringnumber

Source array, string or number to be limited.

limit stringnumber

The length of the returned array or string. If the limit number is positive, limit number of items from the beginning of the source array/string are copied. If the number is negative, limit number of items from the end of the source array/string are copied. The limit will be trimmed if it exceeds array.length. If limit is undefined, the input will be returned unchanged.

begin
(optional)
stringnumber

Index at which to begin limitation. As a negative index, begin indicates an offset from the end of input. Defaults to 0.

Returns

Arraystring

A new sub-array or substring of length limit or less if input array had less than limit elements.

Example

<script>
  angular.module('limitToExample', [])
    .controller('ExampleController', ['$scope', function($scope) {
      $scope.numbers = [1,2,3,4,5,6,7,8,9];
      $scope.letters = "abcdefghi";
      $scope.longNumber = 2345432342;
      $scope.numLimit = 3;
      $scope.letterLimit = 3;
      $scope.longNumberLimit = 3;
    }]);
</script>
<div ng-controller="ExampleController">
  <label>
     Limit {{numbers}} to:
     <input type="number" step="1" ng-model="numLimit">
  </label>
  <p>Output numbers: {{ numbers | limitTo:numLimit }}</p>
  <label>
     Limit {{letters}} to:
     <input type="number" step="1" ng-model="letterLimit">
  </label>
  <p>Output letters: {{ letters | limitTo:letterLimit }}</p>
  <label>
     Limit {{longNumber}} to:
     <input type="number" step="1" ng-model="longNumberLimit">
  </label>
  <p>Output long number: {{ longNumber | limitTo:longNumberLimit }}</p>
</div>