Hello all,
I've done some googling on grouping errors with ng-repeat, but I've
found nothing that solves my problem, so I'm asking here. I'm trying
to use an array of "widgets" to build up a Bootstrap Carousel.
http://rc.getbootstrap.com/javascript.html#carousel
Below is a mockup I've made that works well for what I'm trying to
accomplish. You might notice that rather than including one item in an
"item" div, I'm including two:
<div id="chartCarousel" class="carousel slide">
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0"
class="active"></li>
<li data-target="#myCarousel" data-slide-to="1" class=""></li>
<li data-target="#myCarousel" data-slide-to="2" class=""></li>
</ol>
<div class="carousel-inner">
<div class="item active">
<div class="widget">widget1</div>
<div class="widget">widget2</div>
</div>
<div class="item">
<div class="widget">widget3</div>
<div class="widget">widget4</div>
</div>
<div class="item">
<div class="widget">widget5</div>
<div class="widget">widget6</div>
</div>
</div>
<a class="left carousel-control" href="#chartCarousel" data-
slide="prev">‹</a>
<a class="right carousel-control" href="#chartCarousel" data-
slide="next">›</a>
</div>
After porting this to Angular, I end up with the following HTML:
<div id="chartCarousel" class="carousel slide">
<ol class="carousel-indicators">
<li data-target="#chartCarousel" ng-repeat="widget in widgets
| filter: {type: 'chart'} | groupBy"
data-slide-to="{{$index}}" ng-class="{active: $index ==
0}"></li>
</ol>
<div class="carousel-inner">
<div class="item" ng-repeat="widget in widgets | filter:
{type: 'chart'} | groupBy" ng-class="{active: $index == 0}">
<div class="widget">{{widget[0].title}}</div>
<div class="widget">{{widget[1].title}}</div>
</div>
</div>
<a class="left carousel-control" href="#chartCarousel" data-
slide="prev">‹</a>
<a class="right carousel-control" href="#chartCarousel" data-
slide="next">›</a>
</div>
The groupBy filter is defined as:
angular.module('dashboard.filters', []).
filter('groupBy', function () {
return function (input) {
var groupedItems = [];
$.each(input, function(index, item) {
if (index % 2 == 0) {
var group = [];
group.push(item);
if (index + 1 < input.length) {
group.push(input[index + 1]);
}
groupedItems.push(group);
}
});
return groupedItems;
}
});
This all works like I want it to in Chrome, but I end up with errors
in my console.
Error: 10 $digest() iterations reached. Aborting!
Watchers fired in the last 5 iterations:
Is there a better way to write my groupBy filter or is there something
else I can use in order to group an array of items?
Thanks,
Matt