简体   繁体   中英

load `<select>` drop down default value using AngularJS

i need to load default value for my drop downs.

first drop down

<select class="form-control pz" ng-model="newuser.title">
    <option ng-repeat="x in titles" value="{{x}}">{{x}}</option>
</select>

data for first drop down

$scope.titles = ["MR","MRS","MS"];

I need to load "MR" as a default value in my first drop down.

second drop down with data.

<select class="form-control"  ng-model="newuser.assessmentyear" required
        ng-option="x in assessmentyears">
    <option selected>--Select Year--</option>
    <option ng-repeat="x in assessmentyears" value="{{x}}">{{x}}</option>
</select>

I need to load "--Select Year--" as a default value.

can you help

For the first one, you need to set ng-model value with the value of the first option:

$scope.newuser = {};
$scope.newuser.title = "MR";

For the second dropdown add value="" to set default option as selected:

<option value="">--Select Year--</option>

Note also that this is not needed:

ng-option="x in assessmentyears"

as you are adding options with ng-repeat .

You can see documentation and similar examples here: https://docs.angularjs.org/api/ng/directive/select

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM