AngularJS orderBy
필터
예시
항목을 알파벳순으로 표시:
<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>
예시
"city"를 기준으로 배열을 내림차순으로 정렬합니다.
<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 튜토리얼: 각도 필터