angular.module.ng.$filter.limitTo

Description

Creates a new array containing only a specified number of elements in an array. The elements are taken from either the beginning or the end of the source array, as specified by the value and sign (positive or negative) of limit.

Note: This function is used to augment the Array type in Angular expressions. See angular.module.ng.$filter for more information about Angular arrays.

Usage

limitTo(array, limit);

Parameters

Returns

{Array}

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

Example

  <script>
    function Ctrl() {
      this.numbers = [1,2,3,4,5,6,7,8,9];
      this.limit = 3;
    }
  </script>
  <div ng:controller="Ctrl">
    Limit {{numbers}} to: <input type="integer" ng:model="limit"/>
    <p>Output: {{ numbers | limitTo:limit | json }}</p>
  </div>
  it('should limit the numer array to first three items', function() {
    expect(element('.doc-example-live input[ng\\:model=limit]').val()).toBe('3');
    expect(binding('numbers | limitTo:limit | json')).toEqual('[1,2,3]');
  });

  it('should update the output when -3 is entered', function() {
    input('limit').enter(-3);
    expect(binding('numbers | limitTo:limit | json')).toEqual('[7,8,9]');
  });

  it('should not exceed the maximum size of input array', function() {
    input('limit').enter(100);
    expect(binding('numbers | limitTo:limit | json')).toEqual('[1,2,3,4,5,6,7,8,9]');
  });