2016-07-25 48 views
3

我必須在此循環中獲取問題編號。如何在AngularJS中找到ng-repeat中的循環編號

<div ng-repeat="quizqs in Quizdata.questions"> 
    <li ng-click="form.goTo(Form, 1)"> 
     <a href ng-class="{'selected' : currentStep >= 1, 'done' : currentStep > 1}"> 
      <div class="stepNumber"> 1 </div> 
      <span class="stepDesc text-small">Question No {{quizqs.id}} - {{Quizdata_Lenght}}</span> 
     </a> 
    </li> 
</div> 

回答

3

您可以使用{{$index}}

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js"></script> 
 
<div ng-app> 
 
    <div ng-repeat="quizqs in [12,43,354,13,653]"> 
 
    <li ng-click="form.goTo(Form, 1)"> 
 
     <a href ng-class="{'selected' : currentStep >= 1, 'done' : currentStep > 1}"> 
 
     <div class="stepNumber"> 
 
     </div> <span class="stepDesc text-small">Question No {{$index}}</span> 
 
     </a> 
 
    </li> 
 
    </div> 
 
</div>

0

要獲得一個循環迴路數或環路ID,則AngularJS有被稱爲內置變量 - $index

從0開始 $index,所以每當你想:

$index只能ng-repeat無論是在HTML文件或指令(像filterng-include,自定義指令等)

使用記住從1開始,你需要增加1。下面

代碼:

<div class="stepNumber"> 
      {{index}} 
</div> 
<span class="stepDesc text-small">Question No: {{$index + 1}}</span>  

DEMO