Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access previous and next item in angularjs ng-repeat

I have a list of events that I display using ng-repeat. From each event, I need to access the previous and next event for HTML display reasons. When I don't use filters, this works well using

events[$index - 1]

However, if I filter and re-order the events using

event in events | myMinDate: 'start':config.now | orderBy: 'start'

I cannot use this techique, as the events array still refers to the original (unfiltered, unsorted) data structure. It's unfortunate there is no data array of the filtered data or something like $previous and $next.

Any ideas how this can be achieved without touching the actual events array?

like image 872
Onestone Avatar asked Jul 10 '14 15:07

Onestone


People also ask

Where is the last element in NG-repeat?

$first and $last It's common when using ng-repeat to add specific behavior to the first or last element of the loop, e.g. special styling around the edges. Instead, ng-repeat already supplies you with two ready boolean properties. $first is true for the first element, and $last is true for the last element.

How do you use two ng-repeat in a table?

NEST TWO ng-repeatThe first ng-repeat in the tr tag will create the rows and the second one in the td tag will create one column per element in the collection. By using one ng-repeat for the row and an other for the column, the rows and the columns of your table is now driven by a subset.

How do I get the index of an element in NG-repeat?

Note: The $index variable is used to get the Index of the Row created by ng-repeat directive. Each row of the HTML Table consists of a Button which has been assigned ng-click directive. The $index variable is passed as parameter to the GetRowIndex function.

Does ng-repeat create a new scope?

However, some directives, such as ng-controller and ng-repeat, create new child scopes and attach the child scope to the corresponding DOM element.


1 Answers

You can save the results of the filter into a new variable:

 event in filteredEvents = (events | myMinDate: 'start':config.now | orderBy: 'start')

Then:

filteredEvents[$index - 1]
like image 91
noj Avatar answered Oct 07 '22 00:10

noj