如何通过过滤器对字符串数组进行排序?

时间:2013-01-24 02:49:40

标签: angularjs

以下是无效的代码:演示:http://jsfiddle.net/8dt94/63/

<div ng-controller="MyCtrl">    
    <input type="text" ng-model="searchText" />
  <ul ng-repeat="strVal in arrVal|orderBy|filter:searchText" >
      <li>{{strVal}}</li>
  </ul>
</div>

var app=angular.module('myApp', []);
app.controller('MyCtrl', function ($scope,$filter) {
  $scope.arrVal = ['one','two','three','four','five','six'];  
});

2 个答案:

答案 0 :(得分:242)

您可以通过方法进行排序,因此可以使用toString方法

<ul ng-repeat="strVal in arrVal | orderBy:'toString()' | filter:searchText">

答案 1 :(得分:11)

写一个custom filter

app.filter('mySort', function() {
    return function(input) {
      return input.sort();
    }
  });

HTML:

<ul ng-repeat="strVal in arrVal|filter:searchText|mySort">

Fiddle