Default value in md-autocomplete

0

I'm trying to set a default value to an md-autocomplete, but it's not possible for me.

I need to be able to perform this function, since I have a service that returns certain parameters that should be automatically completed and, if it does not exist, the user completes the form manually.

I put the current code, but of course, I have no default values, because I have not found or if possible (I understand that), but I do not know if the md-autocomplete functionality itself or not.

Vista

<md-autocomplete
   md-selected-item="ciudadItem"
   md-search-text="nombreCiudad"
   md-items="ciudad in obtenerCiudades(nombreCiudad)"
   md-item-text="ciudad.desCiudad"
   ng-model="NombreCiudadCups.valor"
   placeholder="Nombre ciudad..."
   name="Ciudad">
   <md-item-template>
     <span md-highlight-text="nombreCiudad">{{ciudad.desCiudad}}</span>
   </md-item-template>
   <md-not-found>
     Ciudad no encontrada.
   </md-not-found>
</md-autocomplete>

Controller

$scope.obtenerCiudades = function (text) {
    text = text.toUpperCase();
    var ret = $scope.ciudades.filter(function (d) {
        return d.desCiudad.startsWith(text);
    });
return ret;
};

$scope.ciudades is a simple json with a cities, it has no mystery, so I do not attach it.

At this point, all the data is well displayed and searches well, but I need to be able to put a default value, which the user could then erase.

    
asked by Diego 12.07.2018 в 10:59
source

1 answer

0

Solution

First declare in the controlled the empty item.

$scope.ciudadItem = {};

Then, when I receive service value, load it.

$scope.ciudadItem = {
                        desCiudad: {
                            desCiudad: "Esta es la ciudad"
                        }
                    };

To finish, I made a small adjustment in the template, for the value

md-selected-item="ciudadItem.desCiudad"
    
answered by 13.07.2018 в 08:08