Problems with ng-repeat

Asked

Viewed 118 times

0

I have a problem in ng-repeat, can 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>

Code of the Angularjs

$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);
        });
    };

inserir a descrição da imagem aqui

2 answers

2

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

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

0

Browser other questions tagged

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