angularjs获取某列的最大值

时间:2015-01-30 12:14:03

标签: angularjs

我对angularjs有所了解,并阅读有关orderBy的文档 ,想知道如何获得某一列的最大值,

像最年长的人是亚当 - 35

Name (^)    Phone Number    Age
Adam         555-5678       35
Julie        555-8765       29
Mike         555-4321       21
Mary         555-9876       19
John         555-1212       10

示例on plunker

有谁知道怎么做?

谢谢

1 个答案:

答案 0 :(得分:2)

我希望这就是你要找的东西:

var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {
  $scope.friends = [
    {name:'John', phone:'555-1212', age:10},
    {name:'Mary', phone:'555-9876', age:19},
    {name:'Mike', phone:'555-4321', age:21},
    {name:'Adam', phone:'555-5678', age:35},
    {name:'Julie', phone:'555-8765', age:29}
  ];
  $scope.change = function(o) {
    var arr = [];
    angular.forEach($scope.friends, function(item) {
      arr.push(item[o]);
    });
    $scope.result = arr.sort()[0];
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="myApp" ng-controller="MyCtrl">
  <select ng-model="val" ng-change="change(val)">
    <option>name</option>
    <option>phone</option>
    <option>age</option>
  </select>
  <p>sorted first value: {{result}}</p>
</div>