AngularJS Select (Dropdown)
AngularJS Select (Dropdown)
AngularJS can use an array or an object to create a dropdown list option.
Create a select box using ng-options
In AngularJS, we can useng-optiondirectives to create a dropdown list, where list items are output by looping over objects and arrays, as in the following example:
Example
<
div
ng-app
=
"
myApp
"
ng-controller
=
"
myCtrl
"
<
select
ng-init
=
"selectedName = names[0]
"
ng-model
=
"
selectedName
"
ng-options
=
"
x for x in names
"
</
select
</
div
<
scriptvar app = angular.module('myApp', []); app.controller('myCtrl', function($scope) { $scope.names = ["Google", "Runoob", "Taobao"]; });
</
script
Try it »
ng-init sets the default selected value.
ng-options and ng-repeat
We can also useng-repeatdirectives to create a dropdown list:
instance
<
select
<
option
ng-repeat="x in names"
{{x}}
<
/option
<
/selectTry it »
ng-repeatThe directive loops through an array to create a dropdown list in HTML code, butng-optionsThe directive is more suitable for creating dropdown lists, and it has the following advantages:
Useng-optionsThe options ofng-repeatIt is a string.
Which one should be better?
Assume we use the following object:
$scope.sites = [
{site : "Google", url : "http://www.google.com"},
{site : "Runoob", url : "http://www.runoob.com"},
{site : "Taobao", url : "http://www.taobao.com"}
];
ng-repeatIt has limitations, the selected value is a string:
instance
Useng-repeat:
<
select
ng-model=
"selectedSite"
<
option
ng-repeat="x in sites"
value=
"{{x.url}}"
{{x.site}}
<
/option
<
/select
<
h1You selected:
{{selectedSite}}
<
/h1
Try it »
Usageng-optionsdirective, the selected value is an object:
instance
Usageng-options:
<
select
ng-model=
"selectedSite"
ng-options="x.site for x in sites"
<
/select
<
h1You selected:
{{selectedSite.site}}
<
/h1
<
pThe URL is:
{{selectedSite.url}}
<
/p
Try it »
When the selected value is an object, we can get more information, and the application becomes more flexible.
Data source is an object
In the previous example, we used an array as the data source; below, we will use a data object as the data source.
$scope.sites = {
site01 : "Google",
site02 : "Runoob",
site03 : "Taobao"
};
ng-optionsThe usage objects are very different, as shown below:
Instance
Use the object as a data source,xas the key,yas the value:
<
select
ng-model=
"selectedSite"
ng-options="x for (x, y) in sites"
<
/select
<
h1The value you selected is:
{{selectedSite}}
<
/h1
Try it »
The value you select is in key-valuein the pairvalue.
valuein key-valueIt can also be an object in the middle:
instance
The selected value in the key-valuepairvalueis an object here:
$scope.cars = {
car01 : {brand :
"Ford"
, model :
"Mustang"
, color :
"red"
},
car02 : {brand :
"Fiat"
, model :
"500"
, color :
"white"
},
car03 : {brand :
"Volvo"
, model :
"XC90"
, color :
"black"
}
};
Try it »
You can also not use it in the dropdown menukeyin a key-value pairkey, directly use object properties:
instance
<
select
ng-model=
"selectedCar"
ng-options="y.brandfor (x, y) in cars"
<
/select
Try it »