Problems showing data from an array in a select in Angularjs

1

I have the following array in:

  var GruposSanguineos = [
{
    Nombre: '0 RH(+)',
    Valor: '0+'
},
{
    Nombre: '0 RH(-)',
    Valor: '0-'
},
{
    Nombre: 'A RH(+)',
    Valor: 'A+'
},
{
    Nombre: 'A RH(-)',
    Valor: 'A-'
},
{
    Nombre: 'B RH(+)',
    Valor: 'B+'
},
{
    Nombre: 'B RH(-)',
    Valor: 'B-'
},
{
    Nombre: 'AB RH(+)',
    Valor: 'AB+'
},
{
    Nombre: 'AB RH(-)',
    Valor: 'AB-'
},

];

I call the array in my controller like this:

     $scope.datosLicencia = {
        Constantes: {
        GruposSanguineos: GruposSanguineos,
                    },
        NuevosDatos: {},
                             };

I show it in the view in a select as follows:

    <tr>
        <th class="col-md-3 text-right">Grupo Sanguineo</th>
        <td class="col-md-9">
            <select class="form-control" name="GrupoSanguineo" id="GrupoSanguineo" ng-model="datosLicencia.NuevosDatos.GrupoSanguineo" ng-options="GrupoSanguineo.Valor as GrupoSanguineo.Nombre for GrupoSanguineo in datosLicencia.Constantes.GruposSanguineos" required>
                 <option value=""></option>
             </select>
         </td>
    </tr>

I bring the value of the ws select like this:

    WSAPP.ConsultaSOAP(Consulta).then(function (response) {
    if (!response.CodError) {
        $scope.datosLicencia.NuevosDatos.GrupoSanguineo = response.GrupoSanguineo;
    } else {
        bootbox.alert($scope.datos.Errores[response.CodError]);
    }
    $.skylo('end');
});

and I get the value of the select like this:

     Consulta = {
        URL: webServiceLicencias,
        Metodo: 'ActualizarLicencia',
        Parametros: $scope.datosLicencia.NuevosDatos,
    };

The problem is this: when I come into view I would have to show you this way

but it shows me that way

How do I resolve it?

    
asked by Sebastian Kullman 24.08.2016 в 15:45
source

2 answers

3

This solution is very coupled to your problem. What I would do is: receive the value, filter on the original set and set the model based on this. Something like this:

WSAPP.ConsultaSOAP(Consulta).then(function (response) {
  if (!response.CodError) {
    // Primero busco todos los grupos sanguíneos que calcen con el valor de response
    var grupo = GruposSanguineos.filter(function(item) {
      return item.Nombre == response.GrupoSanguineo;
    });
    // Debiese ser solo uno, no? así que le seteo su valor
    $scope.datosLicencia.NuevosDatos.GrupoSanguineo = grupo[0].Valor;

  } else {
      bootbox.alert($scope.datos.Errores[response.CodError]);
  }
});

This way you keep using Valor and do not need to make more changes in other places the code

    
answered by 24.08.2016 / 16:39
source
2

Another option is simply in the ng-model of the select add .Value, thus: ng-model="dataLicencia.NewData.GroupSanguineo.Value" this works if $ scope.datosLicencia.NewData.GroupSanguine is filled (when you run WSAPP.ConsultaSOAP (Query)) with an object that exists in the SanguineosGroups array.

Fix : For the response to work for you.GroupSanguine should be equivalent to the Value in the array Example: response.GroupSanguinee = {"GroupSanguinee": "A +"} and NOT as you fill it at this time {"GroupSanguinee": "A RH (+ ) "} and without adding the .Value as I had indicated.

    
answered by 24.08.2016 в 18:18