I receive data from my back end server structured like this:
{
name : "Mc Feast",
owner : "Mc Donalds"
},
{
name : "Royale with cheese",
owner : "Mc Donalds"
},
{
name : "Whopper",
owner : "Burger King"
}
For my view I would like to "invert" the list. I.e. I want to list each owner, and for that owner list all hamburgers. I can achieve this by using the underscorejs function groupBy
in a filter which I then use in with the ng-repeat
directive:
JS:
app.filter("ownerGrouping", function() {
return function(collection) {
return _.groupBy(collection, function(item) {
return item.owner;
});
}
});
HTML:
<li ng-repeat="(owner, hamburgerList) in hamburgers | ownerGrouping">
{{owner}}:
<ul>
<li ng-repeat="burger in hamburgerList | orderBy : 'name'">{{burger.name}}</li>
</ul>
</li>
This works as expected but I get an enormous error stack trace when the list is rendered with the error message "10 $digest iterations reached". I have a hard time seeing how my code creates an infinite loop which is implied by this message. Does any one know why?
Here is a link to a plunk with the code: http://plnkr.co/edit/8kbVuWhOMlMojp0E5Qbs?p=preview
For what it's worth, to add one more example and solution, I had a simple filter like this:
with:
which caused the described infinite recursion in
$digest
. Was easily fixed with:This is also necessary since
ngRepeat
paradoxically doesn't like repeaters, i.e."foo\n\nfoo"
would cause an error because of two identical paragraphs. This solution may not be appropriate if the contents of the paragraphs are actually changing and it's important that they keep getting digested, but in my case this isn't an issue.This happens because
_.groupBy
returns a collection of new objects every time it runs. Angular'sngRepeat
doesn't realize that those objects are equal becausengRepeat
tracks them by identity. New object leads to new identity. This makes Angular think that something has changed since the last check, which means that Angular should run another check (aka digest). The next digest ends up getting yet another new set of objects, and so another digest is triggered. The repeats until Angular gives up.One easy way to get rid of the error is to make sure your filter returns the same collection of objects every time (unless of course it has changed). You can do this very easily with underscore by using
_.memoize
. Just wrap the filter function in memoize:A resolver function is required if you plan to use different field values for your filters. In the example above, the length of the array is used. A better be to reduce the collection to a unique md5 hash string.
See plunker fork here. Memoize will remember the result of a specific input and return the same object if the input is the same as before. If the values change frequently though then you should check if
_.memoize
discards old results to avoid a memory leak over time.Investigating a bit further I see that
ngRepeat
supports an extended syntax... track by EXPRESSION
, which might be helpful somehow by allowing you to tell Angular to look at theowner
of the restaurants instead of the identity of the objects. This would be an alternative to the memoization trick above, though I couldn't manage to test it in the plunker (possibly old version of Angular from beforetrack by
was implemented?).I am not sure why this error is coming but, logically the filter function gets called for each element for the array.
In your case the filter function that you have created returns a function which should only be called when the array is updated, not for each element of the array. The result returned by the function can then be bounded to html.
I have forked the plunker and have created my own implementation of it here http://plnkr.co/edit/KTlTfFyVUhWVCtX6igsn
It does not use any filter. The basic idea is to call the
groupBy
at the start and whenever an element is addedThanks for the memoize solution, it works fine.
However, _.memoize uses the first passed parameter as the default key for its cache. This could not be handy, especially if the first parameter will always be the same reference. Hopefully, this behavior is configurable via the
resolver
parameter.In the example below, the first parameter will always be the same array, and the second one a string representing on which field it should be grouped by:
Okay, I think I figured it out. Start by taking a look at the source code for ngRepeat. Notice line 199: This is where we set up watches on the array/object we are repeating over, so that if it or its elements change a digest cycle will be triggered:
Now we need to find the definition of
$watchCollection
, which begins on line 360 of rootScope.js. This function is passed in our array or object expression, which in our case ishamburgers | ownerGrouping
. On line 365 that string expression is turned into a function using the$parse
service, a function which will be invoked later, and every time this watcher runs:That new function, which will evaluate our filter and get the resulting array, is invoked just a few lines down:
So
newValue
holds the result of our filtered data, after groupBy has been applied.Next scroll down to line 408 and take a look at this code:
The first time running, oldValue is just an empty array (set up above as "internalArray"), so a change will be detected. However, each of its elements will be set to the corresponding element of newValue, so that we expect the next time it runs everything should match and no change will be detected. So when everything is working normally this code will be run twice. Once for the setup, which detects a change from the initial null state, and then once again, because the detected change forces a new digest cycle to run. In the normal case no changes will be detected during this 2nd run, because at that point
(oldValue[i] !== newValue[i])
will be false for all i. This is why you were seeing 2 console.log outputs in your working example.But in your failing case, your filter code is generating a new array with new elments every time it's run. While this new array's elments have the same value as the old array's elements (it's a perfect copy), they are not the same actual elements. That is, they refer to different objects in memory that simply happen to have the same properties and values. Hence in your case
oldValue[i] !== newValue[i]
will always be true, for the same reason that, eg,{x: 1} !== {x: 1}
is always true. And a change will always be detected.So the essential problem is that your filter is creating a new copy of the array every time it's run, consisting of new elements that are copies of the original array's elments. So the watcher setup by ngRepeat just gets stuck in what is essentially an infinite recursive loop, always detecting a change and triggering a new digest cycle.
Here's a simpler version of your code that recreates the same problem: http://plnkr.co/edit/KiU4v4V0iXmdOKesgy7t?p=preview
The problem vanishes if the filter stops creating a new array every time it's run.
Pardon the brevity, but try
ng-init="thing = (array | fn:arg)"
and usething
in yourng-repeat
. Works for me but this is a broad issue.