I have a duplicate values problem in AngularJS, in my JSON will always have repeated categories, in ng-repeat
I used track by $index
but the error persisted.
My controller code:
angular.module('starter').controller('Guia', function($scope, $http) {
var grp = {};
$http.get('js/lugares.json').success(function(data) {
$scope.categories = data;
angular.forEach($scope.categories, function(item){
var chr = item.categoria.charAt(0);
if(!grp.hasOwnProperty(chr)) grp[chr] = [];
grp[chr].push(item.categoria);
})
});
$scope.grp = grp;
});
My HTML looks like this:
<div class="list" ng-repeat="(k,v) in grp track by $index">
<div class="item item-divider">{{k}}</div>
<a ng-href="#/categoria-guia" class="item" ng-repeat="i in v">{{i}}</a>
</div>
And JSON like this:
[
{
"id": 0,
"nome": "Agência MegaDigital",
"categoria": "Tecnologia e Informática"
},
{
"id": 1,
"nome": "Bar do Grego",
"categoria": "Bares e Restaurantes"
},
{
"id": 2,
"nome": "Anselmo Lanches",
"categoria": "Bares e Restaurantes"
},
{
"id": 3,
"nome": "Internet de Dimas",
"categoria": "Tecnologia e Informática"
},
{
"id": 4,
"nome": "Banca Caixa Postal",
"categoria": "Banca de Revista"
},
{
"id": 5,
"nome": "Xerox Central",
"categoria": "Papelaria e Escritório"
}
]
The final result displays a list of all categories in a list sorted by the initial letter of the category.