2016-01-20 54 views
0

我需要根據值對ng-repeat中的$scope對象進行排序。我可以通過自定義過濾器將它轉換爲array,但只能將鍵或值推入array。我需要這兩個鍵和值顯示它的UI:

$scope.list = { 
    test:58uy43: "test:one", 
    test:24ht76: "test:two", 
    test:26df90: "test:three", 
    test:39fg67: "test:four", 
    test:18ds65: "test:five" 
} 

<div ng-repeat="(key,value) in list">{{key}} : {{value}}</div> 

請幫我在這

+1

你最好有一個對象數組,我認爲 – k102

+1

你可能會發現這個有用:http://stackoverflow.com/questions/14788652/how-to-filter-key-value-with-ng-repeat-in -angularjs – Filipe

回答

1

我認爲,最徹底的方法是定義在控制器中一個函數,返回排序列表。

$scope.getListSorted = function() { 
    $scope.list.sort(function(a, b) { return /*comparison*/; }); 
}; 

對於數值比較應該a.attr - b.attr,如果你想升序排序。
對於字符串值比較應該是a.attr.localeCompare(b.attr)

然後你就可以在視圖

<div ng-repeat="(key,value) in getListSorted()">{{key}} : {{value}}</div> 
1

... NG-重複=使用 「|過濾器O數據:過濾器|排序依據:sortPredicate:sortReverse」 ......

其中「過濾器「是您想要搜索的值(過濾器),'sortPredicate'是您想要排序的列名稱,'sortReverse'是表示記錄順序的布爾值。

相關問題