scope - Angularjs ng-repeat and ng-options are not working in select -
i'm trying values $scope in form using select, undefined values, please me.
angular js
in code, call agregarmuestra() when submit form, $scope.nuevo_muestra doesn't match when using select, works input.
.controller('ctrlregm',function($scope, $state, $http){ $scope.tipos = [{"tipo":"sangre"}, {"tipo":"heces"}, {"tipo":"orina"}]; $scope.nuevo_muestra = {}; $scope.agregarmuestra = function() { console.log($scope.nuevo_muestra); //print empty array console.log($scope.nuevo_muestra.tipo); //print undefined $http.post("/muestra", { tipo: $scope.nuevo_muestra.tipo, }).success(function(response){ $scope.muestras.push(response); $scope.$apply(); }); }; }
html
this doesn't work
<div class="form-group col s12" > <select ng-options="t.tipo t in tipos track t.tipo" ng-model="nuevo_muestra.tipo"> </select> <label>el tipo es: {{nuevo_muestra.tipo}}</label> </div>
this either
<div class="form-group input-field col s12" > <select ng-model="nuevo_muestra.tipo"> <option ng-repeat="t in tipos" value="{{t.tipo}}"> {{t.tipo}} </option> </select> </div>
but if use input, work, why, why?? :(
<div class="form-group"> <input type="text" class="form-control" name="tipo" ng-model="nuevo_muestra.tipo"> </div>
it must either be
<select ng-options="t.tipo t in tipos" ng-model="nuevo_muestra"> </select>
that replace nuevo_muestra selected element of tipos.
or
<select ng-options="t.tipo t.tipo t in tipos" ng-model="nuevo_muestra.tipo"> </select>
that set nuevo_muestra.tipo tipo of selected element of tipos.
this described in the documentation.
also, success()
deprecated long time, , $scope.$apply()
useless here.
Comments
Post a Comment