[英]Angularjs ng-repeat and ng-options are not working in select
I'm trying to get the values from certain $scope in a form using select, but all I get is undefined values, please help me. 我正在尝试使用select从某种形式的$ scope中获取值,但是我得到的只是未定义的值,请帮帮我。
ANGULAR JS 角JS
In this code, I call agregarMuestra() when submit the form, but the $scope.nuevo_muestra doesn't match anything when using select , only works with input . 在这段代码中,我在提交表单时调用agregarMuestra(),但是$ scope.nuevo_muestra在使用select时不匹配任何内容,仅适用于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 的HTML
this doesn't work 这不起作用
<div class="form-group col s12" >
<select ng-options="t.tipo for t in tipos track by 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 I use a input , then it DOES 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 for t in tipos" ng-model="nuevo_muestra">
</select>
That will replace nuevo_muestra by the selected element of tipos. 这将由tipos的选定元素替换nuevo_muestra。
or 要么
<select ng-options="t.tipo as t.tipo for t in tipos" ng-model="nuevo_muestra.tipo">
</select>
That will set nuevo_muestra.tipo with the tipo of the selected element of tipos. 这会将nuevo_muestra.tipo设置为tipos所选元素的tipo。
This is well described in the documentation . 文档中对此进行了详细说明 。
Also, success()
is deprecated for a long time, and $scope.$apply()
is completely useless here. 而且,很长时间以来,不赞成使用success()
,而$scope.$apply()
在这里完全没有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.