文字列の配列でorderbyフィルターを機能させる方法は?


85

動作していないコードは次のとおりです。デモ: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'];  
});

ng-repeat配列でプリミティブ値を使用する必要があるとは思いません。そうでない場合は動作します。(jsfiddle.net/EGVwG)。
Tosh 2013年

この質問は、文字列のリストである必要があるselectのng-optionsattrにも当てはまります。
スペイン列車

回答:


248

メソッドで注文できるので、toStringメソッドを使用できます

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

+1。さらに、最後に「trackby」を追加できます。<ul ng-repeat = "strVal in arrVal | orderBy: 'toString()' | filter:searchText track by $ index">
エイミー

9
素晴らしい解決策です。この[2,5,3,1,6, 33]ように並べ替えられた数値の配列が必要だったので、代わりにftoString()を使用するvalueOf()と、完璧に機能しました。解決してくれてありがとう。
ug_ 2014年

これは私にとってもうまくいきました!このトリックが機能する理由はありますか?
エレサン

弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.