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>
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.
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.
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}}.
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 .
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>
Reached here by searching for something similar.
Here is what worked for me.
{{$index + 1}}
+1 is added because the index starts from 0.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With