AngularJS Select (selection box)

Source: Internet
Author: User

AngularJS can use arrays or objects to create a drop-down list option.

Create a selection box using ng-option

In AngularJS we can use the ng-option directive to create a drop-down list, with list items looping through objects and arrays, as in the following example:

<! DOCTYPE html>

ng-option and ng-repeat:

We can also use the ng-repeat directive to create a drop-down list:

<! DOCTYPE html>

Ng-option and Ng-repeat use which one to make the choice box better? We tend to use ng-option, because ng-repeat has limitations, the selected value is a string, and using the Ng-options directive, the selected value is an object, and when the selection value is an object, we can get more information and the application is more flexible.

The following is a comparison of the examples:

Let's say we use the following data:

$scope. Sites = [    {site: "Google", url: "http://www.google.com"},    {site: "Runoob", url: "Http://www.runoob.com" },    {site: "Taobao", url: "http://www.taobao.com"}];

Using ng-repeat:

<! DOCTYPE html>

Using ng-options:

<! DOCTYPE html>

In the previous example we used an array as the data source, and the following we use the data object as the data source.

$scope. Sites = {    site01: "Google",    site02: "Runoob",    site03: "Taobao"};

ng-options uses objects to be very different, using objects as data sources, x as key (key), and y as value (value):

<! DOCTYPE html>

The value that you select is in value key-value pair .

value can also be an object in the key-value pair:

<! DOCTYPE html>

You can also use the properties of the object directly in the drop-down menu without using the key in the key -value pair:

<! DOCTYPE html>

AngularJS Select (selection box)

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.