1. Select Overview
In AngularJS, you can use the ng-option directive to create a drop-down list, and the list items are output through objects and arrays.
<div ng-app="myApp" ng-controller="myCtrl"> <select ng-model="selectedName" ng-options="x for x in names"> </select> </div> <script> var app = ('myApp', []); ('myCtrl', function($scope) { $ = ["Google", "Runoob", "Taobao"]; }); </script>
2. The data source is an object
The selected value is the key in the key-value pair:
<div ng-app="myApp" ng-controller="myCtrl"> <p>Choose a car:</p> <select ng-model="selectedCar" ng-options="x for (x, y) in cars"> </select> <h1>What you chose is: {{}}</h1> <h2>Model: {{}}</h2> <h3>color: {{}}</h3> <p>Note that the selected value is an object。</p> </div> <script> var app = ('myApp', []); ('myCtrl', function($scope) { $ = { car01 : {brand : "Ford", model : "Mustang", color : "red"}, car02 : {brand : "Fiat", model : "500", color : "white"}, car03 : {brand : "Volvo", model : "XC90", color : "black"} } }); </script>
The selected value is a property of the value object in the key-value pair:
<div ng-app="myApp" ng-controller="myCtrl"> <p>Choose a car:</p> <select ng-model="selectedCar" ng-options=" for (x, y) in cars"> </select> <p>What you chose is: {{}}</p> <p>Model number is: {{}}</p> <p>The color is: {{}}</p> <p>The options in the drop-down list can also be attributes of the object.。</p> </div> <script> var app = ('myApp', []); ('myCtrl', function($scope) { $ = { car01 : {brand : "Ford", model : "Mustang", color : "red"}, car02 : {brand : "Fiat", model : "500", color : "white"}, car03 : {brand : "Volvo", model : "XC90", color : "black"} } }); </script>
3. ng-options and ng-repeat
You can also use the ng-repeat directive to create a drop-down list.
The ng-repeat directive is to loop through an array of HTML code to create drop-down lists, but the ng-options directive is more suitable for creating drop-down lists, and it has the following advantages:
An object using the option of ng-options, ng-repeat is a string.
1) ng-repeat has limitations, and the selected value is a string:
<div ng-app="myApp" ng-controller="myCtrl"> <p>Select a website:</p> <select ng-model="selectedSite"> <option ng-repeat="x in sites" value="{{}}">{{}}</option> </select> <h1>What you chose is: {{selectedSite}}</h1> </div> <script> var app = ('myApp', []); ('myCtrl', function($scope) { $ = [ {site : "Google", url : ""}, {site : "Runoob", url : ""}, {site : "Taobao", url : ""} ]; }); </script>
2) Use the ng-options directive, and the selected value is an object:
<div ng-app="myApp" ng-controller="myCtrl"> <p>Select a website:</p> <select ng-model="selectedSite" ng-options=" for x in sites"> </select> <h1>What you chose is: {{}}</h1> <p>The URL is: {{}}</p> </div> <script> var app = ('myApp', []); ('myCtrl', function($scope) { $ = [ {site : "Google", url : ""}, {site : "Runoob", url : ""}, {site : "Taobao", url : ""} ]; }); </script>
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.