Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the index (counter) of an 'ng-repeat' item with AngularJS?

I am using AngularJS and its ng-repeat directive to display a sequence of questions. I need to number each question starting with 1. How do I display and increment such a counter with ng-repeat? Here is what I have so far:

<ul>     <li ng-repeat="question in questions | filter: {questionTypesId: questionType, selected: true}">         <div>             <span class="name">                 {{ question.questionText }}             </span>         </div>         <ul>             <li ng-repeat="answer in question.answers">                 <span class="name">                     {{answer.selector}}. {{ answer.answerText }}                 </span>             </li>         </ul>     </li> </ul> 
like image 497
lascoff Avatar asked Mar 11 '14 19:03

lascoff


People also ask

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.

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.

What is $Index in AngularJS?

Stay on the bleeding edge of your favorite technologies. $index is a way to show which iteration of a loop you're in. If we set up an ng-repeat to repeat over the letters in 'somewords', like so: <div ng-app="app"> <div ng-repeat="item in 'somewords'.split('')"> {{$index + 1}}.

Where is the last element in NG-repeat?

You can use $last variable within ng-repeat directive. Take a look at doc. Where computeCssClass is function of controller which takes sole argument and returns 'last' or null .


2 Answers

Angularjs documentation is full of examples, you just need to take some time and explore it.

See this example here : ngRepeat example , it's the same case.

<ul>      <li ng-repeat="question in questions | filter: {questionTypesId: questionType, selected: true}">          <div>              <span class="name">                  {{$index + 1}} {{ question.questionText }}              </span>          </div>          <ul>              <li ng-repeat="answer in question.answers">                  <span class="name">                      {{answer.selector}}. {{ answer.answerText }}                  </span>              </li>          </ul>      </li>  </ul>
like image 125
أنيس بوهاشم Avatar answered Oct 18 '22 00:10

أنيس بوهاشم


Reached here by searching for something similar.

Here is what worked for me.

{{$index + 1}} 

+1 is added because the index starts from 0.

like image 36
Yasser Shaikh Avatar answered Oct 18 '22 00:10

Yasser Shaikh