This happens because _.groupBy
returns a collection of new objects every time it runs. Angular's ngRepeat
doesn't realize that those objects are equal because ngRepeat
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:
app.filter("ownerGrouping", function() {
return _.memoize(function(collection, field) {
return _.groupBy(collection, function(item) {
return item.owner;
});
}, function resolver(collection, field) {
return collection.length + field;
})
});
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 the owner
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 before track by
was implemented?).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…