Compare 2 variables in a select with Angular

0
Hello, I have an array of objects that I am pulling from the database and playing in a Select HTML, but I am also passing another variable and would like to compare this variable with the BD array values and if they are same as set the option. I'm using angular. Here is the code I'm using:

<div class="col-xs-12 input_formulario" ng-init="vm.tipoSeguro()">
    <select name="setor_destino" required="" ng-model="user.tipoSeguro" >
        <option value="">Nome do seguro</option>
        <option ng-repeat="seguros in vm.mostraTipoSeguro track by seguros.id" value="{{seguros.nome}}">{{seguros.nome}}</option> 
    </select>
</div>

The ng-model I have there in select is to send the selected value to the function in the controller that sends the form via email.

Followed HTML made with Gabriel's answer Camera:

<select name="setor_destino" required="" ng-model="user.tipoSeguro" >
    <option value="">Nome do seguro</option>
    <option ng-repeat="seguros in vm.mostraTipoSeguro track by seguros.id" value="{{seguros.nome}}" ng-selected="seguros.id == vm.cookie">{{seguros.nome}}</option> 
</select>

And my controller:

vm.tipoSeguro = function () {
    SimulacaoService.listaTipoSeguro().then(function (response) {
        if (response.data != 0) {
            vm.mostraTipoSeguro = response.data;
            vm.cookie = $.cookie("nome_seguro");
            $.removeCookie("nome_seguro");
        } else {
            vm.mostraTipoSeguro = '';
        }
    }, function (error) {
        console.error(error);
    });
}
    
asked by anonymous 03.05.2016 / 13:12

2 answers

0

In order for the select to be selected, the model must have the same value as the value, so the angle will automatically identify that the values are the same and the option will be selected by default.

    
23.07.2017 / 17:26
1

Hello, you can do this by using the ngSelected directive. Home In this JSFiddle I've created an example for you to check out

Editing

I made an adjustment with the AngularJS ngCookies module. Home I guess when you assign $.cookies , it's a jQuery object, in which case you would be pointing to the reference and, I do not know what jQuery does underneath the wads when adding / removing / changing a cookie, but that can be the reason, because after you assign the value, you remove the cookie.

    
03.05.2016 / 13:47