AngularJS Select (Select box) Usage Details, angularjsselect

Source: Internet
Author: User

AngularJS Select (Select box) Usage Details, angularjsselect

1. Select Overview

You can use the ng-option command in AngularJS to create a drop-down list. list items are output cyclically 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 = angular.module('myApp', []);  app.controller('myCtrl', function($scope) {    $scope.names = ["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> select a car: </p> <select ng-model = "selectedCar" ng-options = "x for (x, y) in cars"> </select> 

The selected value is an attribute of the value object in the key-value Pair:

<Div ng-app = "myApp" ng-controller = "myCtrl"> <p> select a car: </p> <select ng-model = "selectedCar" ng-options = "y. brand for (x, y) in cars "> </select> <p> what you choose is: {selectedCar. brand }}</p> <p> model: {selectedCar. model }}</p> <p> color: {selectedCar. color }}</p> <p> the options in the drop-down list can also be object attributes. </P> </div> <script> var app = angular. module ('myapp', []); app. controller ('myctrl ', function ($ scope) {$ scope. cars = {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 command to create a drop-down list.
The ng-repeat command uses arrays to loop through HTML code to create a drop-down list. However, the ng-options command is more suitable for creating a drop-down list. It has the following advantages:
An object that uses the ng-options Option. ng-repeat is a string.

1) ng-repeat has limitations. 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 = "{x. url }}" >{{ x. site }}</option> </select> 

2) using the ng-options command, the selected value is an object:

<Div ng-app = "myApp" ng-controller = "myCtrl"> <p> select a website: </p> <select ng-model = "selectedSite" ng-options = "x. site for x in sites "> </select> 

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.