2019独角兽企业重金招聘Python工程师标准>>>

下拉列表的简单使用

ng-option指令使用很简单,只需要绑定两个属性:

一个是ng-model用于获取选定的值;

另一个是ng-options用于确定下拉列表的元素数组。

<select ng-model="engineer.currentActivity" class="form-control" ng-options="act for act in activities"></select>

上面这条语句就是把选择的值与engineer.currentActivity进行双向数据绑定,然后列表中的选项是activities中的每一个值。数据如下:

$scope.engineer = {name: "Dani",currentActivity: "Fixing bugs"};$scope.activities =["Writing code","Testing code","Fixing bugs","Dancing"];

运行结果如:

为了美观一点,这里引用了bootstrap。

View Code

复杂对象,自定义列表名称

有的时候下拉列表并不是单纯的字符串数组,可能是json对象,例如:

$scope.activities =[{ id: 1, type: "Work" , name: "Writing code" },{ id: 2, type: "Work" , name: "Testing code" },{ id: 3, type: "Work" , name: "Fixing bugs" },{ id: 4, type: "Play" , name: "Dancing" }];

这个时候,绑定的数据就必须是与这里面的格式相同的数据,比如直接复制其中一条:

$scope.engineer = {name: "Dani" ,currentActivity: {id: 3,type: "Work" ,name: "Fixing bugs"}};

当然也可以直接指定成:

$scope.engineer = {currentActivity:activities[3]}

然后在指令中可以循环列表拼接处下拉框的名称

<select ng-model = "engineer.currentActivity"class="form-control"ng-options = "a.name +' (' + a.type + ')' for a in activities" >
</select >

运行效果如:

全部的代码如下:

<html ng-app="myApp">
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><script src="http://apps.bdimg.com/libs/angular.js/1.2.16/angular.min.js"></script><link rel="stylesheet" href="http://apps.bdimg.com/libs/bootstrap/3.3.0/css/bootstrap.min.css">  <script src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script><script src="http://apps.bdimg.com/libs/bootstrap/3.3.0/js/bootstrap.min.js"></script>
</head>
<body><div ng-controller="EngineeringController" class="container"><div class="col-md-12">{{engineer.name}} is currently: {{ engineer.currentActivity}}</div><div class="col-md-4"><label for="name">Choose a new activity:</label>            <select ng-model = "engineer.currentActivity"class="form-control"ng-options = "a.name +' (' + a.type + ')' for a in activities" >               </select > </div></div><script type="text/javascript">var myAppModule = angular.module("myApp",[]);myAppModule.controller("EngineeringController",["$scope",function($scope){$scope.engineer = {name: "Dani" ,currentActivity: {id: 3,type: "Work" ,name: "Fixing bugs"}};$scope.activities =[{ id: 1, type: "Work" , name: "Writing code" },{ id: 2, type: "Work" , name: "Testing code" },{ id: 3, type: "Work" , name: "Fixing bugs" },{ id: 4, type: "Play" , name: "Dancing" }];}]);</script>
</body>
</html>

实现下拉列表的分组

其实分组与前面的例子很像,只要把空间中的ng-options的值换成下面:

<select ng-model = "engineer.currentActivity"class="form-control"ng-options = "a.name group by a.type for a in activities" >
</select >

添加 group by 就会按照后面的值进行分组

全部代码:

<html ng-app="myApp">
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><script src="http://apps.bdimg.com/libs/angular.js/1.2.16/angular.min.js"></script><link rel="stylesheet" href="http://apps.bdimg.com/libs/bootstrap/3.3.0/css/bootstrap.min.css">  <script src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script><script src="http://apps.bdimg.com/libs/bootstrap/3.3.0/js/bootstrap.min.js"></script>
</head>
<body><div ng-controller="EngineeringController" class="container"><div class="col-md-12">{{engineer.name}} is currently: {{ engineer.currentActivity}}</div><div class="col-md-4"><label for="name">Choose a new activity:</label>            <!-- <select ng-model = "engineer.currentActivity"class="form-control"ng-options = "a.name +' (' + a.type + ')' for a in activities" >               </select >  --><select ng-model = "engineer.currentActivity"class="form-control"ng-options = "a.name group by a.type for a in activities" >               </select > </div></div><script type="text/javascript">var myAppModule = angular.module("myApp",[]);myAppModule.controller("EngineeringController",["$scope",function($scope){$scope.engineer = {name: "Dani" ,currentActivity: {id: 3,type: "Work" ,name: "Fixing bugs"}};$scope.activities =[{ id: 1, type: "Work" , name: "Writing code" },{ id: 2, type: "Work" , name: "Testing code" },{ id: 3, type: "Work" , name: "Fixing bugs" },{ id: 4, type: "Play" , name: "Dancing" }];}]);</script>
</body>
</html>

按照id进行标识

由于之前的ng-model相当于初始的时候给设定了一个值。当你选择一个下拉列表选项的时候,就会覆盖掉这个初始值。

所以更多的时候会使用一个id进行标识,这样在初始化赋值的时候,只需要设定一个id就可以了。

$scope.engineer = {currentActivityId: 3};$scope.activities =[{ id: 1, type: "Work" , name: "Writing code" },{ id: 2, type: "Work" , name: "Testing code" },{ id: 3, type: "Work" , name: "Fixing bugs" },{ id: 4, type: "Play" , name: "Dancing" }];

指令可以写成下面的格式

<select ng-model = "engineer.currentActivityId"class="form-control"ng-options = "a.id as a.name group by a.type for a in activities" >
</select >

通过 as 前面的值,就可以确定唯一的一个选项

全部代码如下:

<html ng-app="myApp">
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><script src="http://apps.bdimg.com/libs/angular.js/1.2.16/angular.min.js"></script><link rel="stylesheet" href="http://apps.bdimg.com/libs/bootstrap/3.3.0/css/bootstrap.min.css">  <script src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script><script src="http://apps.bdimg.com/libs/bootstrap/3.3.0/js/bootstrap.min.js"></script>
</head>
<body><div ng-controller="EngineeringController" class="container"><div class="col-md-12">current is: {{ engineer.currentActivityId}}</div><div class="col-md-4"><label for="name">Choose a new activity:</label>            <select ng-model = "engineer.currentActivityId"class="form-control"ng-options = "a.id as a.name group by a.type for a in activities" >               </select > </div></div><script type="text/javascript">var myAppModule = angular.module("myApp",[]);myAppModule.controller("EngineeringController",["$scope",function($scope){$scope.engineer = {currentActivityId: 3};$scope.activities =[{ id: 1, type: "Work" , name: "Writing code" },{ id: 2, type: "Work" , name: "Testing code" },{ id: 3, type: "Work" , name: "Fixing bugs" },{ id: 4, type: "Play" , name: "Dancing" }];}]);</script>
</body>
</html>

参考

Using ngOptions in AngularJS:http://odetocode.com/blogs/scott/archive/2013/06/19/using-ngoptions-in-angularjs.aspx

转载于:https://my.oschina.net/u/2391658/blog/803873

AngularJS 使用ngOption实现下拉列表相关推荐

  1. AngularJS 技术总结

    学习AngularJS,并且能在工作中使用到,算是很幸运了.因此本篇也会搜集各种资料,进行分享. 书籍分享 AngularJS权威指南 常用链接 AngularJS API文档 AngularJS 用 ...

  2. python存数据库c读数据库喷码加工_某喷码机品牌U盘存储的配置文件简记

    U盘下的 KadexMicro 文件夹是喷码机生成的. 其配置文件存储在如上图位置,后缀 .mjt 实为 xml 文件. 内容如: MODE_TEXT 52 38 154 43 52 38 154 4 ...

  3. angularjs ng-option ie issue解决方案

    angularjs ng-option ie issue解决方案 参考文章: (1)angularjs ng-option ie issue解决方案 (2)https://www.cnblogs.co ...

  4. js下拉 selenium_如何使用Python / Selenium webdriver处理Angularjs / Javascript下拉列表?

    我想在Chromium浏览器上使用 Python和Selenium webdriver自动执行一些浏览器任务.我的python脚本已经能够登录,导航到子页面/做一些点击,并在表单中插入一些东西. 我的 ...

  5. angularjs 学期下拉列表指令

    目标效果: 当激活学期后,课程管理首页和增加课程的学期列表默认选中当前激活学期,而编辑课程时默认选中该课程所在学期 最初的想法: service层从后台请求到当前学期返回给c层,然后在c层将当前学期绑 ...

  6. AngularJs 基础教程​ —— Select(选择框)

    为什么80%的码农都做不了架构师?>>>    本文为 H5EDU 机构官方 HTML5培训 教程,主要介绍:AngularJs 基础教程 -- Select(选择框) Angula ...

  7. AngularJS学习篇(十)

    AngularJS Select(选择框) 使用 ng-options 创建选择框 在 AngularJS 中我们可以使用 ng-option 指令来创建一个下拉列表,列表项通过对象和数组循环输出,如 ...

  8. 下拉列表select显示ng-options

    js中如何处理: it-equipment-list-maintenance-create-controller.js 'use strict'; myApp.controller( 'itEquip ...

  9. AngularJS HTML DOM

    AngularJS 为 HTML DOM 元素的属性提供了绑定应用数据的指令.今天就为大家介绍一下AngularJS中一些与HTML DOM操作有关的指令. ng-options 在 AngularJ ...

最新文章

  1. [Kerberos] Java client访问kerberos-secured cluster
  2. undefind_undefined什么意思
  3. 绘制颜色渐变矩形函数
  4. 【机器学习基础】机器学习的损失函数小结
  5. HRESULT:0x80070057 (E_INVALIDARG)的异常的解决方案
  6. r语言 python 书_推荐关于R的几本书
  7. 003.DNS主从正反解析部署
  8. java面向对象程序课本,Java面向对象程序设计
  9. 华为Mate X国行售价曝光,5G网速实测,强悍!
  10. 《Shell 脚本学习指南 》 -- 背景知识与入门 [第一、二章]
  11. HTML打地鼠小游戏代码
  12. 给创业码农的话--如何提升开发效率
  13. win10图片打开方式里没有默认照片查看器的解决方法
  14. 标准库:turtle --- 海龟绘图
  15. 珠峰海拔8848米,现在有足够大的纸,厚度是0.01米,折多少次高度可以超过珠穆朗玛峰。(JavaScript)
  16. SpringCloud禁用Eureka自我保护模式
  17. c\c++: modifier, qualifier, specifier
  18. 软件测试面试题: B/S、C/S、OA 什么意思?
  19. 学大伟业 Day 5 培训总结
  20. CocosCreator + JavaScript游戏开发

热门文章

  1. 安装phpredis扩展
  2. 让我们带着感恩的心生活
  3. “树人杯”暨第三届辽宁科技大学校园程序设计竞赛正赛I 充分利用学习卡(粉)...
  4. C#中的代理(delegate)[转载]
  5. .NET英文技术文章导读(2017-02-09)
  6. (.DS_Store)避免多人提交代码到GitHub上起冲突
  7. Android打Path的方法
  8. 前后端分离实践(试探篇)
  9. UTF-8 BOM(EF BB BF)
  10. 三层代码讲解--第一课