简体   繁体   中英

Skipping items in ng-repeat (not ng-if)

Is there a way I could literally skip a given amount of items when iterating with ng-repeat ?

For example, let's say I have this array:

arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

Is there a way I could do something like

<div ng-repeat="i in arr">
    <div ng-if="i == 3" ng-skip="10">{{i}}</div></br>
</div>

So the output will be:

1
2
13
14
15

And the ng-if won't be executed for values from 3 to 12 .

Being clear here, what I'm asking is for some kind of modification is the iterator's behavior.

EDIT:

Let me clarify even further.

I'm asking for the equivalent of

for (i = 0; i < arr.length; i++) {
    if (arr[i].should_skip_items == true) {
        i += arr[i].number_of_items_to_skip;
    } else {
        //do stuff...
    }
}

You could use $index and ngSwitch as follows:

//skip one item
<div ng-repeat="i in arr" ng-switch="$index % 2 === 0">
    <div ng-switch-when="true">{{i}}</div></br>
</div>

So, what happens here is that if $index % 2 === 0 , that is, the iterable is an even index, it will be shown.

try this...

  ng-repeat="i in arr track by $index" ng-if="$index < 2 || $index > 11"

and remove ng-if and ng-switch from inner DIV element.

这个怎么样?

ng-if="arr.slice(arr.indexOf(3), arr.indexOf(3)+10).indexOf(i) === -1"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM