Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html ng-app="app">
<head>
<meta name="description" content="angular order by with variable field">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.min.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body ng-controller="c">
    <div>
    {{ list | orderBy : field : reverse }}
    </div>
    
    <div>
        <button ng-click="order('id')">Order by id</button>
        <button ng-click="order('name')">Order by name</button>
    </div>
    
    <div>
        order by {{field}} {{reverse ? 'reverse' : ''}}
    </div>
    
    
    
</body>
</html>
 
angular.module('app', []).
controller('c', function($scope, $filter) {
    
$scope.list=[
  {'id' : 15, 'name' : 'aaa', 'thumbnailUrl' : 'some1'},
  {'id' : 12, 'name' : 'ccc', 'thumbnailUrl' : 'some2'},
   {'id' : 3, 'name' : 'bbb', 'thumbnailUrl' : 'some3'}
];
    
$scope.field = 'id';
    
$scope.reverse = false;
    
$scope.order = function(field) {
    $scope.reverse = ($scope.field === field) ? ! $scope.reverse : false;
    $scope.field = field;
};
    
});
Output 300px

This bin was created anonymously and its free preview time has expired (learn why). — Get a free unrestricted account

Dismiss x
public
Bin info
anonymouspro
0viewers