development

orderby 필터가 문자열 배열에서 작동하도록 만드는 방법은 무엇입니까?

big-blog 2020. 9. 17. 08:25
반응형

orderby 필터가 문자열 배열에서 작동하도록 만드는 방법은 무엇입니까?


작동하지 않는 코드는 다음과 같습니다. 데모 : 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'];  
});

메서드별로 주문할 수 있으므로 toString 메서드를 사용할 수 있습니다.

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

맞춤 필터 작성 :

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

HTML :

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

바이올린 .

참고 URL : https://stackoverflow.com/questions/14493116/how-to-make-orderby-filter-work-on-array-of-strings

반응형