Error ng-options in Angularjs

0

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 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;         
});

service

.service('ServCategorias',['$http',function($http){
    this.servicioCategorias = function(){
        return $http.get('url');
    };
}])

I want to know how I make the first value is by default I hope and there is a solution with everything I thank you

    
asked by Dimoreno 02.06.2017 в 23:16
source

1 answer

0

You tried the following:

$scope.hideLoader = FctLoader.show($scope.hideLoader);
ServCategorias.servicioCategorias().success(function(data, status){
   $scope.categorias = data.data.categorias;
   $scope.data.emp_categoria_uno = data.data.categorias[0].nombre_cat; // Agregar esto
})
.error(function(data, status){
   alert(status+' provinicias');
})
.finally(function() {
  $scope.hideLoader = true;         
});

Or in the html:

 ng-init = "data.emp_cat_uno = categorias[0].nombre_cat"

It may be that you do not select the first or a default value, because in the ng-for, the value of the option is the nombre_cat , then when you assign the whole object to the ng-model (ng-model=" data.emp_cat_uno "= categories [0] => full object, not just the name), you can not find it.

I hope it works, anything, we can do a test on a plunker with an object similar to the one you're receiving.

    
answered by 02.06.2017 / 23:24
source