Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
163 views
in Technique[技术] by (71.8m points)

Set default value option in dropdown 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.

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can have a look at this code

<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>

Controller

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

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.

For simplicity and work around here is the link to JSFIDDLE


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...