1

I'm trying to use AngularJS's ng-repeat to check if a value from a previous iteration is the same as the current.

if previous === current

After, the check I would like to update previous to equal current.

previous = current

I have no idea how to update values from inside an ng-repeat in Angular.

Here is an example:

<div ng-repeat="product in Products  | filter: filters[0] | orderBy:'Section'">

      <div ng-if="product.Section !== $parent.previous">
           {{product.Section}}
      </div>

      $parent.previous = product.Section
</div> 

Update:

While this question might still be helpful in some situations, after some thought I realised that what I really want is new objects grouping products by section: How can I group data with an Angular filter?

Community
  • 1
  • 1
user3758291
  • 129
  • 1
  • 8
  • I think we're going to need to see a more complete example of your code to understand what you're trying to do. – S McCrohan Apr 27 '15 at 02:28
  • Take a look here: http://stackoverflow.com/questions/15412897/how-to-obtain-previous-item-in-ng-repeat – S McCrohan Apr 27 '15 at 02:37
  • That's a good solution, but I have found that `$index` does not represent the previous item once sorting has been applied. I have tried using `track by $index` and other values, but that has caused me nothing but trouble so far. – user3758291 Apr 27 '15 at 02:42
  • Interesting. The thing that is occurring to me might work, but is so hackish I refuse to offer it as a real answer. There has to be something better. However, `
    ` will probably work.
    – S McCrohan Apr 27 '15 at 02:59
  • That does work. Thank you. It might be hacky, but I'm desperate enough to roll with it for now. – user3758291 Apr 27 '15 at 03:09
  • As a 'bonus', short-circuit evaluation means it'll only update your previous when you find a new section... – S McCrohan Apr 27 '15 at 03:10

0 Answers0