I'm having trouble using a filter in conjunction with ng-repeat. I have an event json where I want, when selecting an event type in select, the filter returns a new json with only events that have the event type chosen, this is my code
CONTROLLER
$scope.filtro = "";
$scope.eventos = [
{
data: "2016-05-09",
eventos: [1, 5, 3, 7]
},
{
data: "2016-05-08",
eventos: [2, 0]
},
{
data: "2016-05-07",
eventos: [0, 3, 6]
},
{
data: "2016-04-03",
eventos: [1, 6, 7, 8]
},
{
data: "2016-04-02",
eventos: [7]
},
{
data: "2015-01-01",
eventos: [7, 3]
}
];
HTML
<div ng-repeat="(key,evento) in (eventos|filtraEvento:filtro)">
<div><b style="color:red">{{key}}º) - </b>{{evento.data}}</div>
<div>
<span ng-repeat="e in evento.eventos">[{{e}}]</span>
</div>
</div>
<select ng-model="filtro">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
</select>
FILTER
.filter('filtraEvento', ['Util', function (Util) {
return function (eventos, tipoEvento) {
var eventosFiltrado = [];
if (!Util.isNull(tipoEvento)) {
for (var i = 0; i < eventos.length; i++) {
var evento = [];
for (var j = 0; j < eventos[i].eventos.length; j++) {
if (eventos[i].eventos[j] == tipoEvento) {
evento.push(eventos[i].eventos[j]);
}
}
if (evento.length > 0) {
eventosFiltrado.push({
data: eventos[i].data,
eventos: evento
});
}
}
} else {
eventosFiltrado = eventos;
}
return eventosFiltrado;
}
}]);
Unfiltered the system displays normal, but if I choose some filter, besides giving the error:
"Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!"
The system often duplicated the result: Ex: If I choose the filter "2" the displayed result would be:
1º)2016-05-08
1º)2016-05-08
1º)2016-05-08
1º)2016-05-08
1º)2016-05-08
.
.
.
seguido do json original
From now on I thank you for any help, sorry if I did something out of the standard of the site, I'm new here