Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iteration ng-repeat only X times in AngularJs

How can I use ng-repeat like for in Javascript?

example:

<div ng-repeat="4">Text</div> 

I want to iterate with ng-repeat 4 times but how can I do it?

like image 727
Vural Avatar asked Jan 07 '13 14:01

Vural


People also ask

What is Ng-repeat in AngularJS?

AngularJS ng-repeat Directive The ng-repeat directive repeats a set of HTML, a given number of times. The set of HTML will be repeated once per item in a collection. The collection must be an array or an object. Note: Each instance of the repetition is given its own scope, which consist of the current item.

How do I filter in NG-repeat?

The ng-repeat values can be filtered according to the ng-model in AngularJS by using the value of the input field as an expression in a filter. We can set the ng-model directive on an input field to filter ng-repeat values.

How do I get an index value in ng-repeat?

Note: The $index variable is used to get the Index of the Row created by ng-repeat directive. Each row of the HTML Table consists of a Button which has been assigned ng-click directive. The $index variable is passed as parameter to the GetRowIndex function.

Why we use NG-repeat in Angular?

Example# ng-repeat is a built in directive in Angular which lets you iterate an array or an object and gives you the ability to repeat an element once for each item in the collection. To repeat multiple DOM elements by defining a start and an end point you can use the ng-repeat-start and ng-repeat-end directives.


2 Answers

Angular comes with a limitTo:limit filter, it support limiting first x items and last x items:

<div ng-repeat="item in items|limitTo:4">{{item}}</div> 
like image 195
David Lin Avatar answered Oct 13 '22 06:10

David Lin


This is the simplest workaround I could think of.

<span ng-repeat="n in [].constructor(5) track by $index"> {{$index}} </span> 

Here's a Plunker example.

like image 28
ChandrasekarG Avatar answered Oct 13 '22 06:10

ChandrasekarG