例
显示的项目按字母顺序排列:
<div ng-app="myApp" ng-controller="orderCtrl">
<ul>
<li ng-repeat="x
in cars | orderBy">{{x}}</li>
</ul>
</div>
<script>
var
app = angular.module('myApp', []);
app.controller('orderCtrl',
function($scope) {
$scope.cars = ["Dodge", "Fiat",
"Audi", "Volvo", "BMW", "Ford"];
});
</script>
试一试» 定义和用法
在orderBy
过滤器可以让我们数组排序。
默认情况下,字符串按字母顺序排序,和数字数字顺序排序。
句法
{{ array | orderBy :
expression :
reverse }}
参数值
Value | Description |
---|---|
expression | The expression used to determine the order. The expression can be of
type: String: If the array is an array of objects, you can sort the array by the value of one of the object properties. See the examples below. Function: You can create a function to organize the sorting. Array: Use an array if you need more than one object property to determine the sorting order. The array items can be both strings and functions. |
reverse | Optional. Set to true if you want to reverse the order of the array. |
更多示例
例
排序“城市”的数组:
<div ng-app="myApp" ng-controller="orderCtrl">
<ul>
<li ng-repeat="x
in customers | orderBy : 'city'">{{x.name + ", " + x.city}}</li>
</ul>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('orderCtrl', function($scope) {
$scope.customers
= [
{"name" : "Bottom-Dollar
Marketse" ,"city" : "Tsawassen"},
{"name" : "Alfreds Futterkiste", "city" : "Berlin"},
{"name" : "Bon app", "city" : "Marseille"},
{"name" : "Cactus Comidas para llevar", "city" : "Buenos Aires"},
{"name" : "Bolido Comidas preparadas", "city" : "Madrid"},
{"name" : "Around the Horn", "city" : "London"},
{"name" : "B's Beverages", "city" : "London"}
];
});
</script>
试一试» 例
排序“城市”的数组,按降序排列:
<div ng-app="myApp" ng-controller="orderCtrl">
<ul>
<li ng-repeat="x
in customers | orderBy : ' -city'" >{{x.name + ", " + x.city}}</li>
</ul>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('orderCtrl', function($scope) {
$scope.customers
= [
{"name" : "Bottom-Dollar
Marketse" ,"city" : "Tsawassen"},
{"name" : "Alfreds Futterkiste", "city" : "Berlin"},
{"name" : "Bon app", "city" : "Marseille"},
{"name" : "Cactus Comidas para llevar", "city" : "Buenos Aires"},
{"name" : "Bolido Comidas preparadas", "city" : "Madrid"},
{"name" : "Around the Horn", "city" : "London"},
{"name" : "B's Beverages", "city" : "London"}
];
});
</script>
试一试» 相关页面
AngularJS教程: 角过滤器