AngularJS limitTo Filter

Definition and Usage

limitTo The filter returns an array or string containing only the specified number of elements.

When limitTo When used with arrays, the filter returns an array containing only the specified number of items.

When limitTo When used with strings, the filter returns a string containing only the specified number of characters.

When limitTo When used with numbers, the filter returns a string containing only the specified number of digits.

Using negative numbers returns elements starting from the end of the element, rather than from the beginning.

Related Pages

AngularJS Tutorial:Angular Filters

Instance

Example 1

Display only the first three elements of the array:

<div ng-app="myApp" ng-controller="sizeCtrl">
<ul>
<li ng-repeat="x in cars | limitTo : 3">{{x}}</li>
</ul>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('sizeCtrl', function($scope) {
    $scope.cars = ["Audi", "BMW", "Dodge", "Fiat", "Ford", "Volvo"];
});
</script>

Try It Yourself

Example 2

Display the last three elements of the array:

<div ng-app="myApp" ng-controller="sizeCtrl">
<ul>
<li ng-repeat="x in cars | limitTo : -3">{{x}}</li>
</ul>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('sizeCtrl', function($scope) {
    $scope.cars = ["Audi", "BMW", "Dodge", "Fiat", "Ford", "Volvo"];
});
</script>

Try It Yourself

Example 3

Display three elements starting from position 1:

<div ng-app="myApp" ng-controller="sizeCtrl">
<ul>
<li ng-repeat="x in cars | limitTo : 3 : 1">{{x}}</li>
</ul>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('sizeCtrl', function($scope) {
    $scope.cars = ["Audi", "BMW", "Dodge", "Fiat", "Ford", "Volvo"];
});
</script>

Try It Yourself

Example 4

Display the first three characters of the string:

<div ng-app="myApp" ng-controller="sizeCtrl">
<h1>{{txt | limitTo : 3}}</h1>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('sizeCtrl', function($scope) {
    $scope.txt = "Hello, welcome to AngularJS";
});
</script>

Try It Yourself

Example 5

Show the first three digits of the number:

<div ng-app="myApp" ng-controller="sizeCtrl">
<h1>{{phone | limitTo : 3}}</h1>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('sizeCtrl', function($scope) {
$scope.phone = "123456789";
});
</script>

Try It Yourself

Syntax

{{ object | limitTo : limit : begin }}

Parameter

Parameter Description
limit Number, specify the number of elements to return.
begin Optional. Number, specify where to start the limit. The default value is 0.