Creating AngularJS custom filter that actually Sort

前端 未结 1 795
借酒劲吻你
借酒劲吻你 2021-01-27 05:35

I know that in angularJS there is some built in filter: for example

  • But I don\'t want to us

  • 相关标签:
    1条回答
    • 2021-01-27 06:10

      Use Array.sort. Refer to the example below

      var app = angular.module('so', []);
      
      app.controller('MyCtrl', function($scope) {
        $scope.students = [{'name': 'student'}, {'name': 'john'}, {'name': 'austin'}, {'name': 'doe'}];
      });
      
      app.filter('filterByName', function () {
        return function (item) {
            return item.sort((a,b) => a.name.localeCompare(b.name))
        };
      });
      <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.3/angular.min.js"></script>
      
      <div ng-app="so">
        <div ng-controller="MyCtrl">
          <li ng-repeat="student in students | filterByName track by $index">{{student.name}}</li>
         </div>
      </div>

      0 讨论(0)
    提交回复
    热议问题