<h1><code ng:non-bindable="">limitTo</code>
<span class="hint">(filter in module <code ng:non-bindable="">ng</code>
)</span>
</h1>
<div><h2 id="Description">Description</h2>
<div class="description"><p>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 <code>limit</code>.</p>

<p>Note: This function is used to augment the <code>Array</code> type in Angular expressions. See
<a href="api/angular.module.ng.$filter"><code>angular.module.ng.$filter</code></a> for more information about Angular arrays.</p></div>
<h2 id="Usage">Usage</h2>
<div class="usage"><pre class="prettyprint linenums">limitTo(array, limit);</pre>
<h3 id="Parameters">Parameters</h3>
<ul class="parameters"><li><code ng:non-bindable="">array – {Array} – </code>
<p>Source array to be limited.</p></li>
<li><code ng:non-bindable="">limit – {string|Number} – </code>
<p>The length of the returned array. If the <code>limit</code> number is
positive, <code>limit</code> number of items from the beginning of the source array are copied.
If the number is negative, <code>limit</code> number  of items from the end of the source array are
copied. The <code>limit</code> will be trimmed if it exceeds <code>array.length</code></p></li>
</ul>
<h3 id="Returns">Returns</h3>
<div class="returns"><code ng:non-bindable="">{Array}</code>
– <p>A new sub-array of length <code>limit</code> or less if input array had less than <code>limit</code>
elements.</p></div>
</div>
<h2 id="Example">Example</h2>
<div class="example"><h3>Demo Source Code</h3>
<div source-edit="" source-edit-deps="angular.js script.js" source-edit-html="index.html-183" source-edit-css="" source-edit-js="script.js-182" source-edit-unit="" source-edit-scenario="scenario.js-184"></div>
<div class="tabbable"><div class="tab-pane" title="index.html">
<pre class="prettyprint linenums" ng-set-text="index.html-183" ng-html-wrap=" angular.js script.js"></pre>
<script type="text/ng-template" id="index.html-183">
  
  <div ng-controller="Ctrl">
    Limit {{numbers}} to: <input type="integer" ng-model="limit">
    <p>Output: {{ numbers | limitTo:limit }}</p>
  </div>
</script>
</div>
<div class="tab-pane" title="script.js">
<pre class="prettyprint linenums" ng-set-text="script.js-182"></pre>
<script type="text/ng-template" id="script.js-182">
    function Ctrl($scope) {
      $scope.numbers = [1,2,3,4,5,6,7,8,9];
      $scope.limit = 3;
    }
  </script>
</div>
<div class="tab-pane" title="End to end test">
<pre class="prettyprint linenums" ng-set-text="scenario.js-184"></pre>
<script type="text/ng-template" id="scenario.js-184">
  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')).toEqual('[1,2,3]');
  });

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

  it('should not exceed the maximum size of input array', function() {
    input('limit').enter(100);
    expect(binding('numbers | limitTo:limit')).toEqual('[1,2,3,4,5,6,7,8,9]');
  });
</script>
</div>
</div><h3>Demo Source Code</h3>
<div class="well  doc-example-live" ng-embed-app="" ng-set-html="index.html-183" ng-eval-javascript="script.js-182"></div></div>
</div>
