How to mount an ng-repeat using "track by"?

Asked

Viewed 548 times

1

I have the following HTML

<div class="col-md-4">
    <div class="exibeUnidades">
        <table width="400">
            <thead>
                <tr>
                    <td width="100"><b>Profissionais</b></td>
                </tr>
            </thead>
            <tbody>
                <tr ng-repeat="pro in profissionais track by name">
                    <td width="100"><a href="#agendaProfissional/{{pro.idprofissional}}">{{pro.nome}}</a></td>
                    <td width="160">{{pro.funcao}}</td>
                    <td width="160">{{pro.unidade}}</td>
                </tr>
            </tbody>
        </table>
    </div>
</div>

'Cause I’m doing the following consultation at the bank:

<?php
$idestabel = $_GET['idestabel'];

    $getPro=$pdo->prepare("SELECT * FROM unidade INNER JOIN profissional 
                          ON profissional.idunidade = unidade.idunidade WHERE idestabelecimento=:idestabel");
    $getPro=bindValue(":idestabel", $idestabel);
    $getPro->execute();

    while ($linhaPro=$getPro->fetch(PDO::FETCH_ASSOC)) {

        $idunidade = $linhaPro['idunidade'];
        $unidade = $linhaPro['unidade'];
        $idprofissional = $linhaPro['idprofissional'];
        $idunidade = $linhaPro['idunidade'];
        $nome = $linhaPro['nome'];
        $funcao = $linhaPro['funcao'];

        $return[] = array(
            'idprofissional'    => $idprofissional,
            'nome'  => $nome,
            'funcao'    => $funcao,
            'unidade'   => $unidade
        );


    }
?>

My controller:

app.controller("ProfissionaisCtrl", ['$scope', '$http', '$window', '$location', '$rootScope', function ($scope, $http, $window, $location, $rootScope) {

$rootScope.idestabelecimento = localStorage.getItem('idestabelecimento');

var getPro = function(){
    var idestabel = $rootScope.idestabelecimento;
    var opcao = 3 // Buscar profissionais
    $http.get("http://reservacomdomanda.com/areaAdmin/api/admin_estabelecimento/profissionais.php?opcao="+opcao+"&idestabel="+idestabel).success(function(response){
        $scope.profissionais = response;
    })
}

getPro();

}]);

To return this to me: inserir a descrição da imagem aqui

And this is the message that appears on the console: inserir a descrição da imagem aqui

angular.js:12477 Error: [ngRepeat:dupes] Duplicates in a Repeater are not allowed. Use 'track by' Expression to specify Unique Keys. Repeater: pro in profissionais track by name, Duplicate key: Undefined, Duplicate value: b

Return from console.log(Sponse) att:Sorack inserir a descrição da imagem aqui

  • Has any response helped solve the problem and can address similar questions from other users? If so, make sure to mark the answer as accepted. To do this just click on the left side of it (below the indicator of up and down votes).

1 answer

2

ngRepeat - Error - dupes

Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. Repeater: {0}, Duplicate key: {1}, Duplicate value: {2}

Occurs if there are Duplicate Keys in an ngRepeat Expression. Duplicate Keys are banned because AngularJS use Keys to Associate DOM nodes with items

In free translation:

Duplicados na repetição não são permitidos. Use a expressão 'track by' para especificar chaves únicas. Repetidos: {0}, Chave duplicada: {1}, Valor Duplicado: {2}

Occurs if there are duplicate keys in the expression ngRepeat. Duplicate keys are banned because the AngularJS uses keys to associate nodes from DOM with the items.

Your attribute name does not exist in the object, so it is going over and over again as undefined. You may not use an attribute that can be repeated on track by. You can use another field as below:

<tr ng-repeat="pro in profissionais track by pro.idprofissional">

Or use the $index:

<tr ng-repeat="pro in profissionais track by $index">

Or, if you guarantee that the professional name will not repeat itself:

<tr ng-repeat="pro in profissionais track by pro.nome">

Recalling that the track by serves to improve performance when traversing objects in the DOM.

  • Actually, the attribute name does not exist in the object, but name. I’ve tried "<tr ng-repeat="pro in professionals track by idprofissional">" and the error persists. I also tried "<tr ng-repeat="pro in professional track by $index">" the error warning goes missing, but the data does not appear on the kkkk screen either

  • @Gustavosevero in which part you assign the values in the variable profissionais?

  • I updated the post. The professional variable is in the controller.

  • @Gustavosevero puts a console.log(response) before assignment and put in question also, please

  • There you go, there you go.

  • @Gustavosevero tries to change the assignment line for this here to see if it works: $scope.profissionais = JSON.parse(response);

  • @Gustavosevero changed the example of the code. It was missing the pro.. Try the way it is now, doing the favor

  • I had also tried to put "<tr ng-repeat="pro in professional track by pro.idprofessional">" and did not help anything kkkk

  • Thanks for the help Sorack, but I already managed to solve... I was having some errors in api, php.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.