How to make orderby filter work on array of string

2020-01-27 19:31发布

Here is the code which is not working: Demo: 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'];  
});

标签: angularjs
2条回答
Anthone
2楼-- · 2020-01-27 20:22

You can order by a method, so you can use the toString method

<ul ng-repeat="strVal in arrVal | orderBy:'toString()' | filter:searchText">
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2020-01-27 20:25

Write a custom filter:

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

HTML:

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

Fiddle.

查看更多
登录 后发表回答