在下拉AngularJS中设置默认值选项 [英] Set default value option in dropdown angularjs
本文介绍了在下拉AngularJS中设置默认值选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在下拉菜单中设置了默认值,但是我总是看到选择的第一个选项,但是如果我单击下拉菜单,则会设置我想要的值.
I set the default value in a dropdown, but i see always the first option selected, but if i click the dropdown the value that i want is set.
控制器
tantoSvagoApp.controller("ricercaAttivita", function ($scope, $http) {
$scope.selectedRegione = regione;
});
HTML
<select class="trip dark" ng-model="selectedRegione" ng-options="regione.nome as regione.nome for regione in regioni.regionSelector">
<option value="">TUTTE</option>
</select>
推荐答案
您可以看一下此代码
<div ng-app="myapp">
<fieldset ng-controller="FirstCtrl">
<select ng-options="regione.nome as regione.nome for regione in regioni.regionSelector"
ng-model="selectedRegione"></select>
{{ selectedRegione }}
</fieldset>
</div>
控制器
var myapp = angular.module('myapp', []);
myapp.controller('FirstCtrl', function ($scope) {
$scope.selectedRegione = 'Mike';
$scope.regioni = {
'regionSelector':[
{ id: 1, nome: 'John'},
{ id: 2, nome: 'Rocky'},
{ id: 3, nome: 'Mike'},
{ id: 4, nome: 'Ben' }
]
};
});
< option>
是动态创建的,下拉菜单的值由 $ scope.selectedRegione ='Mike';
预先选择,其中引用 nome:
. $ scope.regioni.regionSelector
数组的'Mike'
The <option>
is created dynamically and the value for the dropdown is pre-selected by $scope.selectedRegione = 'Mike';
which reference the nome: 'Mike'
of the $scope.regioni.regionSelector
array.
为简单起见,请在此处找到 JSFIDDLE
For simplicity and work around here is the link to JSFIDDLE
这篇关于在下拉AngularJS中设置默认值选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文