在dropdown angularjs中设置默认值选项

时间:2017-07-03 15:34:58

标签: angularjs ng-options

我在下拉列表中设置了默认值,但我总是看到第一个选项被选中,但如果我点击下拉列表,我想要设置的值。

CONTROLLER

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>

1 个答案:

答案 0 :(得分:0)

您可以查看此代码

<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: 'Mike'数组的$scope.regioni.regionSelector

为简单起见,这里的解决方法是指向JSFIDDLE

的链接