0

I have a ng-repeat element that runs on an array of pictures. Each of the pictures has properties of total likes and shares. I want to add an HTML div to the top 5 heights total likes and shares combined.

How can I do it?

Do I need to calculate the total amount in advance?

Aviv Paz
  • 1,051
  • 3
  • 13
  • 28
  • You definitely should calculate in advance. Doing the math in your template would be redundant, since the same math would be done once for each item in your array. Also, you'll probably want to sort the array so the top items appear first, yeah? Consider adding a rank field to your array items so you can sort them, then style the first five. – Shaun Scovil Jan 02 '16 at 15:46

1 Answers1

0

(1.)Either you run a map on your array of picture objects (preferred) which will crate a 'sumLikeAndShares' property for each picture objects (which is the sum of likes and shares obviously) and then you can run your ng-repeat by that property, (2.)or create a custom sort filter which will order it for you.

(1) How to run ng-repeat by property:

ng-repeat="picture in pictures track by picture.sumLikeAndShares

(2) How to create a sort filter: Custom order using orderBy in ng-repeat

Community
  • 1
  • 1
Iamisti
  • 1,680
  • 15
  • 30