标签:迭代 详解 类型 通用 expr www 引用 .com 元素
select
是 AngularJS 预设的一组directive。下面是其官网api doc给出的用法:AngularJS:select
大意是,select
中的ngOption
可以采用和ngRepeat
指令类似的循环结构,其data source可以是array或者是object。针对两种data source又有衍生的好几种用法。但是官网的例子实在是太少了。
下面是针对几个不太容易理解的用法的例子。
先上controller
<!-- lang: js -->
function selectCtrl($scope) {
$scope.selected = ‘‘;
$scope.model = [{
id: 10001,
mainCategory: ‘男‘,
productName: ‘水洗T恤‘,
productColor: ‘白‘
}, {
id: 10002,
mainCategory: ‘女‘,
productName: ‘圆领短袖‘,
productColor: ‘黑‘
}, {
id: 10003,
mainCategory: ‘女‘,
productName: ‘短袖短袖‘,
productColor: ‘黃‘
}];
}
usage: label for value in array
<select ng-model="selected" ng-options="m.productName for m in model">
<option value="">-- 请选择 --</option></select>
效果:
说明
m.productName
, 其实就是一个 AngularJS Expressionusage: label for value in array
<select ng-model="selected" ng-options="(m.productColor + ‘ - ‘ + m.productName) for m in model">
<option value="">-- 请选择 --</option></select>
效果
说明
usage: label group by group for value in array
<select ng-model="selected" ng-options="(m.productColor + ‘ - ‘ + m.productName) group by m.mainCategory for m in model">
<option value="">-- 请选择 --</option></select>
效果
说明
group by
,通过$scope.model
中的mainCategory
字段进行分组ngModel
的值usage: select as label for value in array
<select ng-model="selected" ng-options="m.id as m.productName for m in model">
<option value="">-- 请选择 --</option></select>
效果
说明
ng-model
的值。在这里,ng-model等于m.id,当ng-model
发生改变的时候,得到的是m.id
的值ngOption针对不同类型的数据源有不同的用法,主要体现在数组和对象上。
数组:
label for value in array
select as label for value in array
label group by group for value in array
select as label group by group for value in array
select as label group by group for value in array track by trackexpr
对象:
label for ( key , value ) in object
select as label for ( key , value ) in object
label group by group for ( key , value ) in object
select as label group by group for ( key , value ) in object
说明:
通用的js代码:
<script> var MyModule = angular.module("MyModule",[]); MyModule.controller("Ctrl",["$scope", function($scope){ $scope.colors = [ {name:‘black‘, shade:‘dark‘}, {name:‘white‘, shade:‘light‘}, {name:‘red‘, shade:‘dark‘}, {name:‘blue‘, shade:‘dark‘}, {name:‘yellow‘, shade:‘light‘} ]; $scope.object = { dark: "black", light: "red", lai: "red" }; }]); </script>
html:
<select ng-model="myColor" ng-options="color.name for color in colors"></select>
效果:
html:
<select ng-model="myColor" ng-options="color.shade as color.name for color in colors"></select>
效果:
label group by group for value in array
html:
<select ng-model="myColor" ng-options="color.name group by color.shade for color in colors"></select>
html:
<select ng-model="myColor" ng-options="color.name as color.name group by color.shade for color in colors">
效果:
html:
<select ng-model="myColor" ng-options="color.name for color in colors track by color.name">
效果:
html:
<select ng-model="obj" ng-options="key for (key, value) in object"></select>
效果:
html:
<select ng-model="obj" ng-options="key as key for (key, value) in object"></select>
效果:
html:
<select ng-model="obj" ng-options="key group by value for (key, value) in object"></select>
html:
<select ng-model="obj" ng-options="key as key group by value for (key, value) in object"></select>
效果:
标签:迭代 详解 类型 通用 expr www 引用 .com 元素
原文地址:http://www.cnblogs.com/s-quan/p/6005031.html