Search code examples
javascriptangularjsangular-filters

Infinite digest error on filtering array of objects


I have an array of objects as follows:

$scope.arr = [
    {'val': true},
    {'val': false},
    {'val': false},
    {'val': true},
    {'val': false},
]

I am using ng-repeat with a filter which sorts the array on the basis of the key 'val' and puts the objects with 'val' set to true before other objects. Here's the filter:

app.filter('ownerFirst', [function() {
    return function(list) {
        list.sort(function(a, b) {
            return a['val'] - b['val'];
        })
        return list;
    }
}])

template:

<div ng-repeat="obj in arr | ownerFirst">

But I am getting an infinite digest error. Why is this happening?


Solution

  • I don't think you need a custom filter, try using orderBy like this:

    <div ng-repeat="obj in arr | orderBy: '-val'">    
    

    You'll need to put the --sign in front of val because you want a descending sort.