Improve this Doc View Source

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 or string, as specified by the value and sign (positive or negative) of `limit`.

## Usage

### In HTML Template Binding

    {{ limitTo_expression | limitTo : limit}}

### In JavaScript

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

### Arguments

Param | Type | Details
--- | --- | ---
input | Array string | Source array or string to be limited.
limit | string number | 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`

### Returns

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

## Example

Edit in Plunker

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

    var numLimitInput = element(by.model('numLimit'));
    var letterLimitInput = element(by.model('letterLimit'));
    var limitedNumbers = element(by.binding('numbers | limitTo:numLimit'));
    var limitedLetters = element(by.binding('letters | limitTo:letterLimit'));

    it('should limit the number array to first three items', function() {
      expect(numLimitInput.getAttribute('value')).toBe('3');
      expect(letterLimitInput.getAttribute('value')).toBe('3');
      expect(limitedNumbers.getText()).toEqual('Output numbers: [1,2,3]');
      expect(limitedLetters.getText()).toEqual('Output letters: abc');
    });

    // There is a bug in safari and protractor that doesn't like the minus key
    // it('should update the output when -3 is entered', function() {
    //   numLimitInput.clear();
    //   numLimitInput.sendKeys('-3');
    //   letterLimitInput.clear();
    //   letterLimitInput.sendKeys('-3');
    //   expect(limitedNumbers.getText()).toEqual('Output numbers: [7,8,9]');
    //   expect(limitedLetters.getText()).toEqual('Output letters: ghi');
    // });

    it('should not exceed the maximum size of input array', function() {
      numLimitInput.clear();
      numLimitInput.sendKeys('100');
      letterLimitInput.clear();
      letterLimitInput.sendKeys('100');
      expect(limitedNumbers.getText()).toEqual('Output numbers: [1,2,3,4,5,6,7,8,9]');
      expect(limitedLetters.getText()).toEqual('Output letters: abcdefghi');
    });