如何使用自定义过滤器对angularjs中的整数数组进行排序?

时间:2014-09-25 08:54:39

标签: angularjs angularjs-filter

这是我的代码,我不知道我在做什么错误???是否有内置的过滤器或函数来排序整数或数字的数组??

<body data-ng-app="app" data-ng-controller="controller1" >
  <div class="container">
    <div class="row">
        <input type="text" ng-model="searchbox" />

        <li ng-repeat="num in numbers|filter:searchbox|orderBy:number">
            {{num}}
        </li>
    </div>
  </div>  

  <script type="text/javascript">
    var app=angular.module('app', []).controller('controller1', ['$scope', function($scope){


      $scope.numbers=[10,8,6,7];

       $scope.number=function(data)
       {
        return data.sort();
       }

    }


    ]);
  </script>

</body>

1 个答案:

答案 0 :(得分:2)

查看此答案orderBy array item value in Angular ng-repeat

这意味着您可以执行以下操作:

app.controller('ctrl', ['$scope', function($scope){
   $scope.numbers = [10,8,6,7];
   $scope.identity = angular.identity;
}]);

<li ng-repeat="num in numbers | orderBy:identity">
   {{num}}
</li>