AngularJS 雙向綁定
angular要做資料的排序也很簡單,像這樣在html內加上另一個ng-model
- Search: <input ng-model="query">
- Sort by:
- <select ng-model="orderProp">
- <option value="name">Alphabetical</option>
- <option value="age">Newest</option>
- </select>
- <ul>
- <li ng-repeat="phone in phones | filter:query | orderBy:orderProp"">
- {{phone.name}}
- <p>{{phone.snippet}}</p>
- </li>
- </ul>
在原本的json內新增一個屬性
- function PhoneListCtrl($scope) {
- $scope.phones = [
- {"name": "Nexus S",
- "snippet": "Fast just got faster with Nexus S.",
- "age": 0},
- {"name": "Motorola XOOM™ with Wi-Fi",
- "snippet": "The Next, Next Generation tablet.",
- "age": 1},
- {"name": "MOTOROLA XOOM™",
- "snippet": "The Next, Next Generation tablet.",
- "age": 2}
- ];
- $scope.orderProp = 'age';
- }
Demo
參考:
Two-way Data Binding
AngularJS入门教程04:双向绑定
留言