Jul 18 2015 Angular學習Day3:ng-init、ng-repeat、input、ng-options ng-init ng-repeat input ng-options ng-init 使用ng-init,將初始值寫在html 1234<div ng-controller="myController" ng-init="product='咖啡'; price='30' "> { { product }} { { price }}</div> 未使用ng-init,將初始值寫在js中 12345var app = angular.module('myApp', []);app.controller('myController', function myController($scope){ $scope.product2 = '三明治'; $scope.price2 = '50';}) ng-repeat 1234567891011121314<body ng-app="myApp"> <div ng-controller="myController"> <ul> <li ng-repeat = "product in products"> { { $index +1 }} . { { product.name }} : { { product.price }} 元 </li> </ul> <p> 共有 { { products.length }} 樣產品 </p> </div></body> 12345678var app = angular.module('myApp',[]);app.controller('myController', function myController($scope){ $scope.products = [ { name: '咖啡', price: 150 }, { name: '三明治', price: 80}, { name: '蛋糕', price: 100} ];}); input input 屬性 12345678name 名稱ng-model 綁定的資料required 限制是否必填ng-required 限制是否必填ng-minlength 限制最小長度ng-maxlength 限制最大長度ng-pattern 限制RegExp格式ng-change 當input的值發生變化的時候執行 1234567891011<body ng-app="myApp"> <div ng-controller="myController"> Value1: <input type="checkbox" ng-model="value1"> <br> Value2: <input type="checkbox" ng-model="value2" ng-true-value="'YES'" ng-false-value="'NO'"> <br/> <p>value1: { { value1 }} </p> <p>value2: { { value2 }}</p> </div></body> 12345var app = angular.module('myApp', []);app.controller('myController', function myController($scope){ $scope.value1 = true; $scope.value2 = 'YES';}); ng-options 12345678<body ng-app="myApp"> <div ng-controller="myController"> <select ng-model="selected" ng-options="product.name for product in products"> <option value="">-- 請選擇 --</option> </select> <p>單價: { { selected.price }} 元</p> </div></body> 12345678var app = angular.module('myApp', []);app.controller('myController', function myController($scope){ $scope.products = [ { name: '咖啡', price: 150 }, { name: '三明治', price: 80}, { name: '蛋糕', price: 100} ];}) Newer Angular學習Day4:ng-switch、ng-change、ng-include、ng-src、ng-href Older RWD課程3 - 產品規劃