2016-04-07 6 views
1

Ich möchte schreiben:Ich möchte Anzeige eines Index in verschachtelten ng-repeat

Text Text

meine Frage wahr

meine Frage wahr

Text Text

meine Frage tion wahr

meine Frage wahr

meine Frage wahr

Text Text

meine Frage wahr

meine Frage wahr

<span ng-repeat="step in steps"> 
    <b>{{step.statement}}</b><br> 
    <span ng-repeat="question in step.questions"> 
     <b>{{??????}}</b> {{question.question}} {{question.response}}<br> 
    </span> 
</span> 

Ich teste mit $ index + 1:. 1 2 1 2 3 1 2

ich mit $ parent teste $ index + 1: 1 1 2 2 2 3 3

aber ich will 1 2 3 4 5 6 7 ...

Antwort

2

Sie können die length der vorherigen Schleifen weiterleiten und zur nächsten $index hinzufügen. Wie folgt aus:

<div ng-init="lastLength=[]"> 
     <span ng-repeat="step in steps" ng-init="lastLength[$index]=step.questions.length"> 
      <b>{{step.statement}}</b><br> 
      <span ng-repeat="question in step.questions" style="padding-left:50px;"> 
       <b>{{lastLength[$parent.$index-1]+$index+1}}</b> {{question.question}} {{question.response}}<br> 
      </span> 
     </span> 
</div> 

Arbeitsbeispiel:

var app = angular.module('xApp', []).controller('xCtrl', function($scope) { 
 
    $scope.steps = [{ 
 
    statement: 'St 1', 
 
    questions: [{ 
 
     question: 'q 1', 
 
     response: 'rs 1' 
 
    }, { 
 
     question: 'q 2', 
 
     response: 'rs 2' 
 
    }] 
 
    }, { 
 
    statement: 'St 2', 
 
    questions: [{ 
 
     question: 'q 4', 
 
     response: 'rs 4' 
 
    }, { 
 
     question: 'q 5', 
 
     response: 'rs 5' 
 
    }, { 
 
     question: 'q 6', 
 
     response: 'rs 6' 
 
    }] 
 
    }] 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="xApp" ng-controller="xCtrl" ng-init="lastLength=[]"> 
 
    <span ng-repeat="step in steps" ng-init="lastLength[$index]=step.questions.length"> 
 
     <b>{{step.statement}}</b><br> 
 
     <span ng-repeat="question in step.questions" style="padding-left:50px;"> 
 
      <b>{{lastLength[$parent.$index-1]+$index+1}}</b> {{question.question}} {{question.response}}<br> 
 
     </span> 
 
    </span> 
 
</div>