0

Having issues with using the limitTo filter through javascript

The angularjs docs for limitTo state that the filter is to be used in the following way:

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

please note the begin parameter is optional.

Following from the above I did the following:

var myApp = angular.module('myApp',[]);

function MyCtrl($scope,$filter) {
   $scope.numbers =[1,2,4,1,8,11,345,123,908,223,4646,132,24,43,3,76,432,1];
   $filter('limitTo')($scope.numbers, 3);   
}

<div ng-controller="MyCtrl">
 {{numbers}}
</div>

I expect my output to be

[1,2,4]

Yet the whole array was shown meaning the limitTo filter didn't work.

What am I doing wrong?

Link to jsfiddle

1 Answer 1

4

Angular's $filter doesn't modify the original array, so you have to do it like this:

$scope.filteredArray = $filter('limitTo')($scope.numbers, 3);
Sign up to request clarification or add additional context in comments.

3 Comments

This works and I reckon an example in their docs to illustrate this would have been useful.
Creates a new array or string containing only a specified number of elements. It says so in the first sentence on the page ;)
My bad... I often just skip to the examples. Won't do that again.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.