I am consuming a Ws which returns an array, and I present it in a select, the data is displayed well but I want to know how I give it a default value, this is what I have done
<div class="form-group">
<label for="sel1">Categoría 1:</label>
<select class="form-control"
ng-init = "data.emp_cat_uno = categorias[0]" //no funciona
ng-options="c.nombre_cat as c.nombre_cat for c in categorias "
ng-model="data.emp_cat_uno"
ng-change = "setear_id_cat_1()">
</select>
</div>
js
$scope.hideLoader = FctLoader.show($scope.hideLoader);
ServCategorias.servicioCategorias().success(function(data, status){
$scope.categorias = data.data.categorias;
})
.error(function(data, status){
alert(status+' provinicias');
})
.finally(function() {
$scope.hideLoader = true;
});
services
.service('ServCategorias',['$http',function($http){
this.servicioCategorias = function(){
return $http.get('url');
};
}])
I want to know how I make the first value default, I hope and there is a solution with everything, thank you
Did you try the following:
Or in the html:
It might not select the first one or a default value, because in the ng-for, the value of the option is the
nombre_cat
, so when you assign the entire object to the ng-model (ng-model="data.emp_cat_uno" = categories [0] => entire object, not just the name), can't find it.I hope it works, anything, we can do a test on a plunker with an object similar to what you're getting.