I need an item from my select to be selected, Angular

Asked

Viewed 1,410 times

0

I have a select component

<div class="form-group col-md-4">
    <label>Entidade:</label> 
        <select ng-model="distrito.entidade.idEntidade" class="form-control">
        <option value="{{dis.entidade.idEntidade}}" ng-repeat="dis in distritos">{{dis.entidade.nome}}</option>
        </select>
</div>

My controller class

app.controller("buscaDistritoController", function($scope, $http) {


    $scope.distritos = [];
    $scope.distrito = {}; // binding com o form

    carregarDistritos = function() {

        token = localStorage.getItem("userToken");


        $http({
            method : 'GET',
            url : 'http://localhost:8080/user/distritos'
        }).then(function(response) {
            $scope.distritos = response.data;

        }, function(response) {
            console.log(response.data);
            console.log(response.status);
        });
    };



    $scope.salvarDistritos = function() {
        if ($scope.frmDistrito.$valid) {
            $http({
                method : 'POST',
                url : 'http://localhost:8080/user/distritos',
                data : $scope.distrito
            }).then(function(response) {
                carregarDistritos();
                $scope.distrito = {};

            }, function(response) {
                console.log(response.data);
                console.log(response.status);
            });

        } else {
             alert("Campos com * são de preenchimento obrigatório");

        }
    };

    $scope.excluirDistritos = function(distrito) {
        bootbox.confirm({
            message : "Deseja excluir o registro permanentemente ?",
            buttons : {
                cancel : {
                    label : '<i class="fa fa-times"></i> Cancelar'
                },
                confirm : {
                    label : '<i class="fa fa-check"></i> Sim'
                }
            },
            callback : function(result) {
                if (result == true) {
                    $http({
                        method : 'DELETE',
                        url : 'http://localhost:8080/user/distritos/'+distrito.id
                    }).then(function(response) {

                        pos = $scope.distritos.indexOf(distrito);
                        $scope.distritos.splice(pos, 1);

                    }, function(response) {
                        console.log(response.data);
                        console.log(response.status);
                    });
                }
            }
        });
    };

    $scope.alterarDistritos = function(dis) {
        $scope.distrito = angular.copy(dis);
    };

    $scope.cancelarAlteracaoDistritos = function() {
        $scope.distrito = {};
    };

    carregarDistritos();

});

I want to always be a selected option, currently shows only one option visible within select and other options that are blank.

inserir a descrição da imagem aqui

what I need is exactly this but only with an option and always this option selected and without the repetition of selects of course.

inserir a descrição da imagem aqui

  • Edit your question and put your controller please, just this I can’t help

  • @Jackson Edited question !

  • OK @Eduardo, what’s the API answer?

  • @Jackson or Response is via JSON of an entity array

  • @Jackson I reedited the question there.. maybe it got better to understand what I need.

  • I understand what you want, can you pass me what comes from your HTTP Request call? What comes from http://localhost:8080/user/districts ?

  • I get a Json &#Xa Array;{idDistrito: 3, nome: "Distrito de Taquari", codigoDne: "00002454", flagAtivo: 1, entidade: {…}, …}&#xA;&#xA;{idDistrito: 45, nome: "testeee", codigoDne: "00000009", flagAtivo: null, entidade: 1, …}&#xA;&#xA;{idDistrito: 46, nome: "salvando distrito", codigoDne: "000008", flagAtivo: null, entidade: 1, …}

  • I answered the question with a plunker, I did the fixed JSON structure assuming what it would look like. Because JSON there is not complete

Show 3 more comments

1 answer

1


Eduardo,

The ideal to use in this case is the ng-options, it is a component of Angularjs made especially for popular a Select.
Either way, you NG-MODEL comes zeroed by default, you need to initialize it so it comes something selected.

Plunker Example using NG-OPTIONS

Read more about ng-options here

Any doubt I’m available.

Browser other questions tagged

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