Page 89 - AngularJS权威教程
P. 89
9.2 在指令中使用子作用域 69
Decrement 1
</button>
<div>
angular.module('myApp',[]) 2
.controller('CounterController', function($scope) {
$scope.decrement = function() {
$scope.count = $scope.count - 1; 3
};
})
在线示例:http://jsbin.com/uGipUBU/2/edit。 4
18. ng-select
5
ng-select用来将数据同HTML的<select>元素进行绑定。这个指令可以和ng-model以及
ng-options指令一同使用,构建精细且表现优良的动态表单。
6
ng-options的值可以是一个内涵表达式(comprehension expression),其实这只是一种有趣
的说法,简单来说就是它可以接受一个数组或对象,并对它们进行循环,将内部的内容提供给
select标签内部的选项。它可以是下面两种形式。 7
数组作为数据源:
8
用数组中的值做标签;
用数组中的值作为选中的标签;
用数组中的值做标签组; 9
用数组中的值作为选中的标签组。
对象作为数据源: 10
用对象的键值(key-value)做标签;
用对象的键值作为选中的标签; 11
用对象的键值作为标签组;
用对象的键值作为选中的标签组。 12
下面看一个ng-select指令的实例:
13
<div ng-controller="CityController">
<select ng-model="city"
ng-options="city.name for city in cities">
<option value="">Choose City</option> 14
</select>
Best City: {{ city.name }}
</div> 15
angular.module('myApp',[])
.controller('CityController',function($scope) {
$scope.cities = [ 16
{name: 'Seattle'},
{name: 'San Francisco'},
{name: 'Chicago'}, 17
{name: 'New York'},
{name: 'Boston'}
]; 18
});