Problems with ng-repeat

0

I have a problem with ng-repeat, can you help me please?

php code

public function listar_extras() {
    $data = json_decode(file_get_contents("php://input"));

    $tamanho = $data->tamanho;
    $sPizza = $data->sPizza;
    $cont = 0;
    for ($x = 0; $x < count($sPizza); $x++) {
        $nome = $sPizza[$x]->prod_nome;
        $result = $this->pedidos_painel_model->listar_extras($tamanho, $sPizza[$x]->prod_id);
        for ($i = 0; $i < count($result); $i++) {
            if ($result[$i]->ins_nome !== null && $result[$i]->ins_nome !== '') {
                $tamanho = count($result[$i]->ins_nome);
                if ($tamanho > 38) {
                    $nome_cortado = substr(0, 36) + '...';
                } else {
                    $nome_cortado = $result[$i]->ins_nome;
                }
                $extras[$cont]['extra_id'] = $result[$i]->extra_id;
                $extras[$cont]['extra_valor'] = $result[$i]->extra_valor;
                $extras[$cont]['ins_id'] = $result[$i]->ins_id;
                $extras[$cont]['ins_nome'] = $result[$i]->ins_nome;
                $extras[$cont]['ins_nome_cortado'] = $nome_cortado;
                $extras[$cont]['prod_cl_nome'] = $result[$i]->prod_cl_nome;
                $extras[$cont]['prod_nome'] = $result[$i]->prod_nome;
                $extras[$cont]['existe'] = true;

                $cont++;
            } else {
                $extras2[$cont]['prod_nome'] = $nome;
                $extras2[$cont]['msg'] = 'Não há alterações para este sabor!';
                $extras2[$cont]['existe'] = false;
                $cont++;
            }
        }
    }

    $extras[$cont] = $extras2;
    ?><pre><?php
    print_r($extras);
    exit();


    if (count($extras) === 0) {
        $extras = 0;
    }
    echo json_encode($extras);
}

Repeat code

<section  ng-repeat="(key, value) in saboresComExtras| groupBy:'prod_nome'">
                        <div class="col-md-12 padding-zero">
                            <md-subheader class="md-primary">{{key}}</md-subheader>
                            <md-list ng-if="extra.existe" ng-repeat="extra in value|orderBy:'ins_nome'">
                                <div class="col-md-6 padding-zero">
                                    <md-list-item class="md-1-line">
                                        <div class="md-list-item-text font-text padding-zero" style="width: 96%;">
                                            <md-checkbox id="" ng-checked="" ng-click="" aria-label="selecionado" class="md-primary" ng-value="">
                                                <div class="col-md-9 padding-right-zero">
                                                    {{extra.ins_nome_cortado}}
                                                </div>
                                                <div class="col-md-3 padding-zero">
                                                    <div class="pull-right">R$ {{extra.extra_valor}}</div>
                                                </div>
                                            </md-checkbox>
                                            <div class="clearfix"></div>
                                        </div>
                                        <md-tooltip md-direction="top">
                                            {{extra.ins_nome}}
                                        </md-tooltip>
                                    </md-list-item>
                                </div>
                                <div ng-if="!extra.existe" class="col-md-6 padding-zero">
                                    <md-list-item class="md-1-line">
                                        <div class="md-list-item-text font-text padding-zero" style="width:96%">
                                            <div class="col-md-12 padding-right-zero">
                                                {{extra.extra_valor}}
                                            </div>
                                            <div class="clearfix"></div>
                                        </div>
                                    </md-list-item>
                                </div>
                            </md-list>
                        </div>
                    </section>

Angularity code

$scope.carregarExtras = function () {
        $http.post(url_sistema + 'pedidos_painel/listar_extras', {'tamanho': $scope.tamanho, 'sPizza': $scope.sPizza}).success(function (data, status, headers, config) {
            $scope.saboresComExtras = data;
        }).error(function (data, status) { // called asynchronously if an error occurs
            console.log(data);
        });
    };

    
asked by anonymous 03.03.2016 / 18:41

2 answers

2

This error happens because some value is being repeated, eg a name that is similar in more than one object. To solve this, simply use track by and set it to a single value, such as $index . Remember that you can define by a field of your array too, as long as it is unique. For example, an id.

ng-repeat="(key, value) in saboresComExtras| groupBy:'prod_nome' track by $index"
    
03.03.2016 / 18:48
0

It's not exactly the same thing, but I used this technique for ng-repeat: How to send form data received from an ng-repeat with angularjs

    
04.03.2016 / 22:43