GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • 首页
  • HTML
  • JavaScript
  • CSS
  • React
  • Python3
  • Java
  • C
  • C++
  • C#
  • Go
  • SQL
  • Linux
  • VS Code

This view

  • Original page
  • New address

GeezerVison is showing www.runoob.com — AngularJS Select(选择框) | 菜鸟教程

菜鸟教程 -- 学的不仅是技术,更是梦想!

  • Vue3 教程
  • Vue2 教程
  • Bootstrap3
  • Bootstrap4
  • Bootstrap5
  • 机器学习
  • PyTorch
  • TensorFlow
  • Sklearn
  • NLP
  • AI Agent
  • Ollama
  • Coding Plan
AngularJS 教程
AngularJS 教程 AngularJS 简介 AngularJS 表达式 AngularJS 指令 AngularJS 模型 AngularJS Scope(作用域) AngularJS 控制器 AngularJS 过滤器 AngularJS Service AngularJS Http AngularJS Select AngularJS 表格 AngularJS SQL AngularJS HTML DOM AngularJS 事件 AngularJS 模块 AngularJS 表单 AngularJS 输入验证 AngularJS API AngularJS Bootstrap AngularJS 包含 AngularJS 动画 AngularJS 依赖注入 AngularJS 路由 AngularJS 应用

AngularJS 实例

AngularJS 实例

AngularJS 参考手册

AngularJS 参考手册
AngularJS ng-value 指令
AngularJS 服务(Service)

AngularJS Select(选择框)

AngularJS 可以使用数组或对象创建一个下拉列表选项。


使用 ng-options 创建选择框

在 AngularJS 中我们可以使用 ng-option 指令来创建一个下拉列表,列表项通过对象和数组循环输出,如下实例:

实例

<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> <script> var app = angular.module('myApp', []); app.controller('myCtrl', function($scope) { $scope.names = ["Google", "Runoob", "Taobao"]; }); </script>

尝试一下 »

ng-init 设置默认选中值。


ng-options 与 ng-repeat

我们也可以使用ng-repeat 指令来创建下拉列表:

实例

<select>
<option ng-repeat="x in names">{{x}}</option>
</select>

尝试一下 »

ng-repeat 指令是通过数组来循环 HTML 代码来创建下拉列表,但 ng-options 指令更适合创建下拉列表,它有以下优势:

使用 ng-options 的选项是一个对象, ng-repeat 是一个字符串。


应该用哪个更好?

假设我们使用以下对象:

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

ng-repeat 有局限性,选择的值是一个字符串:

实例

使用 ng-repeat:

<select ng-model="selectedSite">
<option ng-repeat="x in sites" value="{{x.url}}">{{x.site}}</option>
</select>

<h1>你选择的是: {{selectedSite}}</h1>

尝试一下 »

使用 ng-options 指令,选择的值是一个对象:

实例

使用 ng-options:

<select ng-model="selectedSite" ng-options="x.site for x in sites">
</select>

<h1>你选择的是: {{selectedSite.site}}</h1>
<p>网址为: {{selectedSite.url}}</p>

尝试一下 »

当选择值是一个对象时,我们就可以获取更多信息,应用也更灵活。


数据源为对象

前面实例我们使用了数组作为数据源,以下我们将数据对象作为数据源。

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

ng-options 使用对象有很大的不同,如下所示:

实例

使用对象作为数据源, x 为键(key), y 为值(value):

<select ng-model="selectedSite" ng-options="x for (x, y) in sites">
</select>

<h1>你选择的值是: {{selectedSite}}</h1>

尝试一下 »

你选择的值为在 key-value 对中的 value。

value 在 key-value 对中也可以是个对象:

实例

选择的值在 key-value 对的 value 中, 这时它是一个对象:

$scope.cars = {
car01 : {brand : "Ford", model : "Mustang", color : "red"},
car02 : {brand : "Fiat", model : "500", color : "white"},
car03 : {brand : "Volvo", model : "XC90", color : "black"}
};

尝试一下 »

在下拉菜单也可以不使用 key-value 对中的 key , 直接使用对象的属性:

实例

<select ng-model="selectedCar" ng-options="y.brand for (x, y) in cars">
</select>

尝试一下 »
AI 思考中...
AngularJS ng-value 指令
AngularJS 服务(Service)

4 篇笔记 写笔记

  1. #0

       烈焰灬

      747***[email protected]

    12

    页面下拉框初始化是空的,设置初始值方法:

    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"}
        }
        $scope.selectedCar = $scope.cars.car02;  //设置第2个为初始值;
    });

    尝试一下 »

    烈焰灬

       烈焰灬

      747***[email protected]

    9年前 (2017-09-13)
  2. #0

       youlangta

      724***[email protected]

    4

    另外一种设置初始值的方法:

    <div ng-app="myApp" ng-controller="myCtrl">
        <select ng-init="selectPerson=persons['caohui']" ng-model="selectPerson"
        ng-options="x for (x,y) in persons">
        </select>
        <p>
            年龄:{{selectPerson.age}}
        </p>
        <p>
            性别:{{selectPerson.sex}}
        </p>
        <p>
            月薪:{{selectPerson.salary}}
        </p>
    </div>

    尝试一下 »

    youlangta

       youlangta

      724***[email protected]

    9年前 (2017-09-17)
  3. #0

       骑鱼看大海

      114***[email protected]

    2

    给 id 一个别名 y.name,显示出来的 name 值,但实际还是 id 值。

    <div ng-app="myApp" ng-controller="myCtrl">
    <p>给 id 一个别名 y.name,显示出来的name值,但实际还是 id 值。</p>
    <select ng-init="selectPersonid=persons['caohui'].id" ng-model="selectPersonid"  ng-options="y.id as y.name for (x,y) in persons">
    </select>
    <p>
        id:{{selectPersonid}}
    </p>
    </div>

    尝试一下 »

    骑鱼看大海

       骑鱼看大海

      114***[email protected]

    8年前 (2018-08-21)
  4. #0

       kindaries

      kin***[email protected]

    6

    下拉框默认的值为 key-value 对中的 value ,也可以用 as 来修改下拉框的值。

    也就是 x for (x, y) 相当于是 y as x for (x, y)。

    as 前面的为下拉框的值,后面的为下拉框显示的内容。

    ​也可以不使用 key-value 对中的 value 作为下拉框的值, 直接使用对象的属性,代码示例:

    <body>
        <div ng-app="myApp" ng-controller="myCtrl">
        
        <p>使用 ng-options 创建下拉列表,选中的值是一个对象:</p>
        <select ng-model="selectedSite" ng-options="y.url as x for (x, y) in sites">
        </select>
        <h4>你选择的是: {{selectedSite}}</h4>
    
    </div>
        
    <script>
        var app = angular.module('myApp', []);
        app.controller('myCtrl', function($scope) {
            $scope.sites = {
                site01 : {site : "Google", url : "http://www.google.com"},
                site02 : {site : "Runoob", url : "https://www.runoob.com"},
                site03 : {site : "Taobao", url : "http://www.taobao.com"}
            };
            $scope.selectedSite = $scope.sites.site02.url;  //设置第2个为初始值;
        });
    </script>
    </body>

    尝试一下 »

    kindaries

       kindaries

      kin***[email protected]

    8年前 (2018-10-08)

点我分享笔记

取消

  • 昵称 (必填)
  • 邮箱 (必填)
  • 引用地址
分类导航
  • Python / 数据科学
    • Python 教程
    • Python2.x 教程
    • FastAPI 教程
    • Flask 教程
    • Django 教程
    • NumPy 教程
    • Pandas 教程
    • SciPy 教程
    • Matplotlib 教程
    • Dash 教程
    • Jupyter Notebook 教程
    • Pillow 教程
    • 量化交易
    • R 教程
    • Julia 教程
  • AI / 智能开发
    • AI Agent(智能体)
    • AI(人工智能)
    • AI 应用案例
    • Codex 教程
    • Vibe Coding 教程
    • Claude Code
    • OpenCode
    • Skills(技能)
    • Ollama 教程
    • Hermes Agent
    • DeepSeek Harness
    • Pi Agent
    • AI 数学基础
    • TensorFlow 教程
    • PyTorch 教程
    • Scikit-learn 教程
    • 机器 教程
    • LangChain
    • 自然语言处理 NLP
    • OpenCV 教程
    • Selenium 教程
    • Playwright 教程
  • 前端开发
    • HTML 教程
    • HTML5 教程
    • CSS 教程
    • CSS3 教程
    • JavaScript 教程
    • HTML DOM 教程
    • TypeScript 教程
    • AJAX 教程
    • JSON 教程
    • Tailwind CSS 教程
    • Bootstrap4 教程
    • Bootstrap5 教程
    • Foundation 教程
    • Vue.js 教程
    • Vue3 教程
    • React 教程
    • Next.js 教程
    • Three.js 教程
    • AngularJS 教程
    • Angular 教程
    • jQuery 教程
    • jQuery UI 教程
    • jQuery EasyUI 教程
    • ECharts 教程
    • Chart.js 教程
    • Highcharts 教程
    • Google 地图 教程
    • SVG 教程
    • Font Awesome 教程
  • 后端开发
    • Node.js 教程
    • Electron 教程
    • PHP 教程
    • Java 教程
    • Go 教程
    • Rust 教程
    • C# 教程
    • Unity 教程
    • Servlet 教程
    • JSP 教程
    • ASP 教程
    • AppML 教程
    • VBScript 教程
    • Swagger 教程
    • RESTful API 教程
    • Docker 教程
    • Linux 教程
    • ZooKeeper 教程
  • 数据库
    • SQL 教程
    • MySQL 教程
    • PostgreSQL 教程
    • SQLite 教程
    • MongoDB 教程
    • Redis 教程
    • Memcached 教程
  • 移动开发
    • Android 教程
    • Flutter 教程
    • Ionic 教程
    • jQuery Mobile 教程
    • Swift 教程
    • Kotlin 教程
  • DevOps / 工程化
    • Git 教程
    • SVN 教程
    • CMake 教程
    • Maven 教程
    • VS Code 教程
    • Obsidian 教程
    • PyCharm 教程
    • Eclipse 教程
    • Markdown 教程
    • LaTeX 教程
  • 编程语言
    • C 教程
    • C++ 教程
    • Zig 教程
    • Scala 教程
    • Ruby 教程
    • Perl 教程
    • Lua 教程
    • Dart 教程
    • 汇编语言 教程
    • Verilog 教程
  • 计算机基础
    • 计算机组成原理
    • 数据结构与算法
    • C 语言数据结构与算法
    • 设计模式
    • Python 设计模式
    • 正则表达式
    • HTTP 教程
    • TCP/IP 教程
    • 网络协议
    • W3C 教程
  • XML / Web Service
    • XML 教程
    • DTD 教程
    • XML DOM 教程
    • XSLT 教程
    • XPath 教程
    • XQuery 教程
    • XLink 教程
    • XPointer 教程
    • XML Schema 教程
    • XSL-FO 教程
    • Web Service 教程
    • WSDL 教程
    • SOAP 教程
    • RSS 教程
    • RDF 教程
  • .NET
    • ASP.NET 教程
    • MVC 教程
    • Razor 教程
    • Web Forms 教程
    • Web Pages 教程
    • PowerShell 教程
  • 网站建设
    • 网站建设指南
    • 浏览器信息
    • 网站主机教程
    • 网站品质

字节 Coding Plan 限时 9.9 元起,支持 Deepseek-V4、GLM-5.3、Doubao-Seed-Evolving、Kimi-K3。
百度 Dumate 百度推出的桌面级 AI 智能体。 输入激活码送 4000 积分
使用文档
Trae AI 原生开发环境,让编程更高效。新用户注册赠2500积分,管理项目,也交付成果。
Advertisement
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.