2
I have a problem of duplicate values in Angularjs, in my JSON will always have repeated categories, in ng-repeat
I used the track by $index
but the error persisted.
The code of my controller:
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 is 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 the JSON so:
[
{
"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 category letter.
The only problem you’re having is sorting by the initial letter?
– Samir Braga
No, in relation to sorting I have already managed to do and it’s all right, the problem is that as I have repeated categories, this error appears here: [ngRepeat:dupes] Duplicates in a Repeater are not allowed. Use 'track by' Expression to specify Unique Keys.
– Altemberg Andrade