Value attribute of options in select are not set correctly

Asked

Viewed 528 times

3

I need to go through an element select on a page HTML placing the elements option within it. I can already do that, the problem is that in the attribute value of those option should receive the identifiers coming from the database, but other numbers are appearing other than the value of the records and when the form is sent occurs persistence error in the Java because the identifier does not exist to make the foreign key interface.

HTML:

<div class="input-group">
    <select name="objeto.oid" id="objetoSelect"
        ng-init="carregarObjeto()" ng-model="objeto"
        class="form-control"
        placeholder="<spring:message code="label.objeto" />"
        aria-describedby="basic-addon1"
        ng-options="objeto.oid as objeto.descricao for objeto in list.objetos">
    </select>
    <span class="input-group-addon" id="basic-addon1">
        <span class="glyphicon glyphicon-pencil"></span>
    </span>
</div>

Javascript:

$scope.carregarObjetos = function() {
    $http({
        method : "GET",
        url : '<c:url value="/cadastros/objeto/getObjetos" />'
    }).then(function mySuccess(response) {
        var length = response.data.length;
        var data = response.data;
        for (var i = 0; i < length; i++) {
            $scope.list.objetos.push({
                oid : data[i].oid,
                descricao : data[i].descricao.toString(),
                label : data[i].modelo.label.toString()
            });
        }
    }), function myError(response) {
        alert("Não foi possível carregar lista de objetos");
    }
}

Upshot:

Instead of showing the numbers 3 and 5 both of us options which comes from the database table, is showing this way:

inserir a descrição da imagem aqui

And the data is correctly received on AJAX.

EDIT

Appears a option thus within the select when updating with the method carregarObjetos:

<option value="? number:5 ?"></option>
  • java or javascript?

1 answer

2


I don’t see why your code doesn’t work, it seems to be ok. What might be happening is getting the data wrong. Have you checked it out?

When using the select you have 2 options. A traditional (as you did) and also using the ng-repeat (show).


Traditional

The way you did it is correct and the result should not be interfered with. The reason why you see number:3 is due to a change that the Angular suffered by switching to version 1.4. This can be removed using a track by.

<select name="teste01" ng-model="teste1" ng-options="item.id as item.nome for item in vm.lista track by item.id"></select>

ng-repeat

I particularly avoid using this as it is a "work-Around", but... In any case, you can also set the options using a ng-repeat, example:

<select name="teste02" ng-model="teste2">
    <option ng-repeat="item in vm.lista" value="{{item.id}}">{{item.nome}}</option>
</select>

Despite the options, the way you are using should be fixed when using the option track by. Here’s an example I did that works correctly with the 3 models: What you did, what you use track by and with ng-repeat:

https://plnkr.co/edit/4cpbq3?p=preview

Browser other questions tagged

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