AngularJS - orderBy in Ctrl
by manoj
HTML
<div ng-controller="MyCtrl">
I have {{friends.length}} friends. They are:
<ul>
<li ng-repeat="friend in sortedFriends">
<ng-switch on="$first || friend.age != sortedFriends[$index-1].age">
<div ng-switch-when="true" class="group">Group of {{friend.age}} years old. Previous: <i>{{sortedFriends[$index-1].age}}</i></div>
</ng-switch>
<b>[{{$index + 1}}]</b> {{friend.name}} who is {{friend.age}} years old.
</li>
</ul>
</div>
CSS
body { margin: 4px }
.group { margin-top: 1em; border-bottom: 1px solid #CCC; }
/*.ng-scope { margin: 4px; border: 1px dashed red; }*/
JavaScript
function MyCtrl($scope, orderByFilter) {
$scope.friends = [{
name: 'John',
age: 25},
{
name: 'Jimmy',
age: 25},
{
name: 'Mary',
age: 28},
{
name: 'Ed',
age: 27},
{
name: 'Andrew',
age: 27}];
$scope.sortedFriends = orderByFilter($scope.friends, '+age');
}