javascript - Is it possible to ng-repeat incrementing $index by an arbitrary number? -


i have template:

        <div class="choice-pair" ng-repeat="choicepair in pastchoices track $index" ng-init="p1result = getp1result(choicepair); choicenum = $index + 1;">             <div class="turn-count cell"><strong>{{choicenum}}</strong></div>             <div class="p1 cell" ng-class="{win: p1result === 1, lose: p1result === -1, draw: p1result === 0}"><strong>{{choicepair[0]}}</strong></div>             <div class="p2 cell" ng-class="{win: p1result === -1, lose: p1result === 1, draw: p1result === 0}"><strong>{{choicepair[1]}}</strong></div>         </div> 

pastchoices array containing sub arrays of length 2 example:

[['a', 'b'],['b', 'c'],['a', 'a'],['c', 'a']] 

but due need change data storage type on server have had flatten structure pastchoices inlined:

['a', 'b', 'b', 'c', 'a', 'a', 'c', 'a'] 

is possible me make small change template iterate on new pastchoices format incrementing 2, can select out choice pairs like:

        <div class="choice-pair" ng-repeat="choices in pastchoices track $index" ng-init="p1result = getp1result(pastchoices[$index], pastchoices[$index + 1]);">             <div class="turn-count cell"><strong>{{$index + 1}}</strong></div>             <div class="p1 cell" ng-class="{win: p1result === 1, lose: p1result === -1, draw: p1result === 0}"><strong>{{pastchoices[$index]}}</strong></div>             <div class="p2 cell" ng-class="{win: p1result === -1, lose: p1result === 1, draw: p1result === 0}"><strong>{{pastchoices[$index + 1]}}</strong></div>         </div> 

this looks me data want, except iterate on every element of pastchoices array, want skip odd numbered elements.

since you're not using choices, can limit amount of results half total in ng-repeat:

<div class="choice-pair" ng-repeat="choices in (pastchoices | limitto: pastchoices.length / 2 ) track $index ">     <div class="turn-count cell"><strong>{{($index * 2) + 1}}</strong></div>     <div class="p1 cell" ng-class="{win: p1result === 1, lose: p1result === -1, draw: p1result === 0}"><strong>{{pastchoices[($index * 2)]}}</strong></div>     <div class="p2 cell" ng-class="{win: p1result === -1, lose: p1result === 1, draw: p1result === 0}"><strong>{{pastchoices[($index * 2) + 1]}}</strong></div> </div> 

https://docs.angularjs.org/api/ng/filter/limitto

this fail if array not length.


Comments