user1679941
问问题
175 次
2 回答
0
很大程度上取决于您使用的数据,以及您希望用户如何排序,但我在这个 jsFiddle 中创建了一个基本示例,您可以使用:http: //jsfiddle.net/xRkhc/2/
HTML:
<div ng-controller="MyCtrl">
<div ng-repeat="row in gdata | orderBy:sort">
{{row.model}}
</div>
<select ng-model="sort">
<option value="model">Sort Up by Model</option>
<option value="-model">Sort Down by Model</option>
<option value="topSpeed">Sort Up by TopSpeed</option>
<option value="-topSpeed">Sort Down by TopSpeed</option>
</select>
</div>
控制器:
function MyCtrl($scope) {
$scope.sort = "model"; //just to set the default sort in the dropdown, not required
$scope.gdata = [{'model':'M3','topSpeed':'200'},{'model':'Type R','topSpeed':'150'}, {'model':'S2000','topSpeed':'160'},{'model':'NSX','topSpeed':'180'}];
}
于 2013-10-03T22:28:31.597 回答
0
当您硬编码选项(如在您的代码中)时,模型只接收字符串或数字,您必须使用ng-options
(或等效的data-ng-options
)而不是硬编码选项。您将选项指定为模型的成员。
这是一个完整的例子:
<!doctype html>
<html data-ng-app>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script>
<script>
var exampleCtrl = function ($scope) {
$scope.sortingDirection = {
'Sort Up' : true,
'Sort Down' : false
};
$scope.inverse = true;
}
</script>
</head>
<body>
<div data-ng-controller="exampleCtrl">
<select data-ng-model="inverse" data-ng-options="value as key for (key, value) in sortingDirection">
</select>
<pre data-ng-bind="inverse | json"></pre>
</div>
</body>
</html>
于 2013-10-03T22:29:00.923 回答