ng-options ng-repeat的用法

工作中总会遇到关于下拉选项的一些操作,但是经常会遇到一些问题,基本上都是以下问题:

- 下拉框使用ng-options和ng-repeat的区别
- 下拉框的默认选项的问题
- 下拉框的model值如何绑定
- 下拉框的禁用选项问题
- 下拉框的分组和排序的问题

ng-repeat和ng-options的用法 
ng-repeat是通过数组来循环HTML代码来创建下拉列表,ng-repeat绑定到ngModel的值只能是字符串。 
ng-options的列表项可以是对象和数组循环输出,很多时候使用ng-options更方便。

(1)看一个数据的列表项是数组的例子:

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

a. 使用ng-options循环数据:

<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>

注意,这里的ng-init可以直接将下拉列表的第一个值设置为默认值。

b. 使用ng-repeat循环数据:

<div ng-app="myApp" ng-controller="myCtrl">
<select>
<option ng-repeat="x in names">{{x}}</option>
</select>
</div>

(2)数据的列表项是对象

$scope.sites = [
{site : "Google", url : "http://www.google.com"},
{site : "Runoob", url : "http://www.runoob.com"},
{site : "Taobao", url : "http://www.taobao.com"}
];
$scope.selectedSite=$scope.sites[0]; //这一句直接将下拉列表的第一个值作为默认值

a. 使用ng-repeat就会有局限性,选择的值只能是一个字符串:

<select ng-model="selectedSite">
<option ng-repeat="x in sites" value="{{x.url}}">{{x.site}}</option>
</select> <h1>你选择的是: {{selectedSite}}</h1>

b. 使用ng-options选择的值就是一个对象:

<select ng-model="selectedSite" ng-options="x.site for x in sites">
</select> <h1>你选择的是: {{selectedSite.site}}</h1>
<p>网址为: {{selectedSite.url}}</p>

从上面两个例子,可以看出ng-options和ng-repeat的区别有以下几个方面:
1. ng-options一定要和ng-model搭配,这里的ng-model获取的就是列表的一个对象。
ng-repeat的value值类型是string。
2. 设置首选项有两个方法,ng-init和直接js赋值,也可以添加一个未选中的选项:

<select ng-model="selectedSite" ng-options="x.site for x in sites">
<option value="">-- 选择一个地址 --</option>
</select>

关于ng-repeat的其他用法

在实际的应用场景中,我们需要一个表格来管理信息,这时候使用ng-repeat的$index可以知道是哪一行被操作了。具体看一个例子:

<table>
<tr ng-repeat="x in list">
<td>{{x.name}}</td>
<td><input type="button" value="我是第{{$index}}行的按钮"
ng-click="onClick($index)"></td>
</tr>
</table>
$scope.onClick = function (index) {
alert("点击了第"+index+"行的按钮");
};

要注意的是,$index是从0开始计算的。

ng-options的分组和排序的功能

(1) ng-options支持group by语法进行分组。

$scope.colors = [
{name: '黑色', color: 'black', type: "暗色"},
{name: '白色', color: 'white', type: "亮色"},
{name: '红色', color: 'red', type: "暗色"},
{name: '蓝色', color: 'blue', type: "暗色"},
{name: '黄色', color: 'yellow', type: "亮色"}
];
<select ng-model="colorChosen" ng-options="color.name group by color.type for color in colors">

结果会根据“暗色”和“亮色”分组。
(2)ng-options支持orderBy过滤器对结果排序

<select ng-model="colorChosen" ng-options="color.name group by color.type for color in colors | orderBy:'name' ">
</select>

结果会根据“暗色”和“亮色”分组,在分组里根据字符顺序排序。

ng-options禁用某些选项

$scope.colors = [
{name: '黑色', color: 'black', type: "暗色"},
{name: '白色', color: 'white', type: "亮色", disabled: false},
{name: '红色', color: 'red', type: "暗色", disabled: true},
{name: '蓝色', color: 'blue', type: "暗色", disabled: false},
{name: '黄色', color: 'yellow', type: "亮色", disabled: true}
];
<select ng-model="colorChosen"
ng-options="color.name group by color.type
disable when color.disabled for color in colors">

ng-select ng-options ng-repeat的用法与区别的更多相关文章

  1. 不知道张(zhāng)雱(pāng)是谁?你out了!

    张(zhāng)雱(pāng)是谁?也许你已经听说过了,也许你还没听说过呢,不过你一定听说过老刘——刘强东,没错,这二人是有关系的,什么关系,京东是老刘的,而张雱呢?张雱是京东旗下52家关联公司法人代 ...

  2. KnockoutJS Select 标签 Options绑定

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  3. vue h render function & render select with options bug

    vue h render function & render select with options bug https://github.com/xgqfrms/vue/issues/41 ...

  4. form:select form:options 标签数据回显

    在jsp页面中经常会使用到 form:select form:options 遍历后台List集合生成 select 下拉选择器,但是 form:options 标签并没有提供一个可以回显数据的属性. ...

  5. select * from 多张表的用法

    select * from 多张表的用法   其实就是 inner join   select * from Class c,Student s where c.ClassID=s.ClassID   ...

  6. Oracle 变量 之 define variable declare 用法及区别

    Oracle 变量 之 define variable declare 用法及区别 Table of Contents 1. 扯蛋 2. define和accept 3. variable 3.1. ...

  7. 【转载】Delphi异常处理try except语句和try finally语句用法以及区别

    Delphi异常处理try except语句和try finally语句用法以及区别 一直写程序都没管他们,也尽量很少用,今天终于想把他给弄个明白,在网上找来,记下!主要是前面小部分,后面的是详细说明 ...

  8. mysql find_in_set 与 in 的用法与区别,mysql范围搜索,mysql范围查询

    mysql find_in_set 与 in 的用法与区别 1.find_in_set 用于模糊查询,并且数据库中的值是用英文逗号分隔的: 例如: (1).去字段中查询 select find_in_ ...

  9. [转载]jQuery中wrap、wrapAll和wrapInner用法以及区别

    原文地址:jQuery中wrap.wrapAll和wrapInner用法以及区别作者:伊少君 原文: <ul>   <li title='苹果'>苹果</li>   ...

  10. WordPress翻译中 __()、_e()、_x、_ex 和 _n 的用法及区别

    编译函数 WordPress使用了下面几个函数来方便语言本地化. __() _e() _x() _ex() _n() 以上所列的函数是用来包含所需翻译的字符串的,根据字符串的不同参数和输出类型,需要使 ...

随机推荐

  1. Java 设计模式_复合模式(2016-08-31)

    一.什么是复合模式? 在形式上,复合模式确实是多个模式的组合,但满足了这一条并不一定是复合模式,注意它的定义: 将多个模式结合起来形成一个“框架”,以解决一般性问题 一提到“框架”,可能最容易联想到的 ...

  2. Linux的cat、more、less的区别

    cat命令功能用于显示整个文件的内容单独使用没有翻页功能因此经常和more命令搭配使用,cat命令还有就是将数个文件合并成一个文件的功能. more命令功能:让画面在显示满一页时暂停,此时可按空格健继 ...

  3. Windows Phone 之文件下载进度和速度显示

    用http协议来下载网络上的文件,通常我们需要获取文件的下载进度和下载的速度来给用户等待过程的一个交代,那么在windows phone 7下可以使用WebClient类来实现这一功能,HttpWeb ...

  4. etTimeout与setInterval方法的区别

    etTimeout与setInterval方法的区别 setTimeout()用于设定在指定的时间之后执行对应的函数或代码.,在全局作用域下执行 setTimeout(code,time[,args… ...

  5. java基础部分

    1.java的基本数据类型及所占的字节 boolen  8位  1个字节 byte 8位 1个字节 char 16位 2个字节 short 16位 2个字节 int 32位 4个字 float 32位 ...

  6. mysql update不能直接使用select的结果

    在sql server中,我们可是使用以下update语句对表进行更新:update a set a.xx= (select yy from b) ;但是在mysql中,不能直接使用set selec ...

  7. js常用字符串函数

    // JS字符串 //1.replace字符串替换,只能换第一部分,就是说多个字符相同,只能换下最先的 var str='helloworld!'; alert(str.replace('llo',' ...

  8. Laravel框架——任务调度(cron)

    准备: 在服务的/var/spool/cron/root文件中添加代码 cd /var/spool/cron/root 添加以下代码 * * * * * phppath 项目路径/artisan sc ...

  9. css:中文词不断开,整体换行

    一.问题   关于文字的换行与不换行的问题有些特殊情况,是使用css的word-break等属性实现不了的,下面的情况就证明了: 我们想要的效果是,一个词整体换行或不换行,“兼职测试”可以都换至第二行 ...

  10. bzoj 2115: [Wc2011] Xor xor高斯消元

    2115: [Wc2011] Xor Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 797  Solved: 375[Submit][Status] ...