1. 程式人生 > >AngularJS學習筆記(二)

AngularJS學習筆記(二)

fan pre 數據源 ron margin -h round mona 一個

一.AngularJS Select(選擇框)

1.使用 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>

<script>

var app = angular.module(‘myApp‘, []);

app.controller(‘myCtrl‘, function($scope) {

$scope.names = ["Google", "Runoob", "Taobao"];

});

</script>

2.使用 ng-repeat 指令來創建下拉列表

<div ng-app="myApp" ng-controller="myCtrl">

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

</div>

<script>

var app = angular.module(‘myApp‘, []);

app.controller(‘myCtrl‘, function($scope) {

$scope.names = ["Google", "Runoob", "Taobao"];

});

</script>

3.使用了數組作為數據源: x 為鍵(key), y 為值(value)
<div ng-app="myApp" ng-controller="myCtrl">

<p>選擇一輛車:</p>

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

<h1>你選擇的是: {{selectedCar.brand}}</h1>

<h2>模型: {{selectedCar.model}}</h2>

<h3>顏色: {{selectedCar.color}}</h3>

<p>註意選中的值是一個對象。</p>

</div>

<script>

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

} });

</script>

AngularJS學習筆記(二)