How to use ng-repeat with filter and $index?

前端 未结 6 1316
南笙
南笙 2020-12-08 07:33

I want to use ng-repeat in Angular, while I only want to output some elements of the array. An Example:

ng-repeat=\"item in items | filter:($index%3 == 0)\"
         


        
6条回答
  •  暖寄归人
    2020-12-08 07:56

    An alternate approach to solving this creates a new "list" (I don't know the proper term in this context) in the ng-repeat. This new list can then be referred to within the scope

    • [{{$index + 1}}] {{filteredFriends[$index].name}} who is {{filteredFriends[$index].age}} years old. [{{$index + 2}}] {{filteredFriends[$index+1].name}} who is {{filteredFriends[$index+1].age}} years old.

    The ng-if on the span wrapping the second half prevents the base text from being shown when the previous element is the last element of the list.

提交回复
热议问题