I
I
IvanKalinin2016-06-26 09:02:53
JavaScript
IvanKalinin, 2016-06-26 09:02:53

How to make ng-repeat work from the end of an array while preserving its order?

There is a specific case where you want ng-repeat to run from the end instead of from the beginning.
That is, let's say there is an array of elements (for example, links to images) ['url1', 'url2, 'url3'....]
Default

<img ng-repeat="img in images track by $index" ng-src="{{img}}" />
add the DOM of each img in order, starting at url1, using something similar to insertAfter(); Is it possible to make it work backwards so that it adds url3 first, then url2 BEFORE url3, etc. Those. kept the order but worked like insertBefore();
images.slice().reverse() does not work in this case, because reverses the order in the array, and all I need to do is reverse the render order. I hope the question is clear.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Сергей Протько, 2016-06-26
@Fesor

Есть специфический случай, когда нужно, чтобы ng-repeat работал с конца, а не сначала.

Сделайте в контроллере revert массива. Не решайте подобное на уровне шаблонов. Ну либо напишите фильтр.

L
lega, 2016-06-26
@lega

Сергей Протько правильно посоветовал, но если лень, то можно так изратиться

<img ng-repeat="img in images track by $index" ng-src="{{images[images.length - $index -1]}}" />

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question