0

Main idea:

I want to combine bootstrap grid system with angular ng-repeat

Method:

I used filter to reformat the json data (an array with a lot of objects), like the codepen project:

http://codepen.io/maggiben/pen/sfCnq ``` filter('listToMatrix', function() { return function listToMatrix(list, elementsPerSubArray) {

    var matrix = [], i, k;
    console.log("hellowrld")
    for (i = 0, k = -1; i < list.length; i++) {
        if (i % elementsPerSubArray === 0) {
            k++;
            matrix[k] = [];
        }

        matrix[k].push(list[i]);
    }

    return matrix;
};

});

enter image description here

And here is my jade page code:

enter image description here

My controller code: To get the news. enter image description here

The problem is I got much more items on the page: enter image description here

daniel-xu
  • 25
  • 7

1 Answers1

0

This error means angular doesn't manage to finish a digest cycle. The reason for it comes from your filter: every time angular applies the filter to your list, a new matrix is created, so angular will keep invoking the filter until what it returns is the same as the previous iteration (which never happens).

To fix it, you could either track the items of your matrix using ng-repeat ... track by items.someProperty, so after two consecutives calls to your filter, angular will detect that this someProperty has not changed and will finish the cycle.

Another way to fix it would be to cache the result of your filter for a given list, so the next time angular invoyes your filter with the same list, you would return a pointer to the same matrix.

For more lecture you can refer to this question (same problem): Angular filter works but causes "10 $digest iterations reached"

Community
  • 1
  • 1
floribon
  • 19,175
  • 5
  • 54
  • 66
  • the problem is: the result is not correct. There's too many items be showed on the page – daniel-xu Feb 05 '15 at 02:24
  • You're facing a XY issue, the problem is what I say, which leads to uncontrolled behaviors. Note that a simpler solution could be to sort your lists in your controller once for all and then remove your filter: you would also get better performances. – floribon Feb 05 '15 at 03:08