2017-02-17 7 views
1

私は多次元配列をJSONに解析しています。私はAngularJSで "order by"したいと思います。そして私はこのエラーを取得しています。エラー:[[並べ替え:notarray]期待の配列が、受信:ここでは13AngularJS多次元配列Order by

<div ng-repeat="x in thread track by x.Id | orderBy: x.Id"> 

は私のJSONです:

{ 
    "13": { 
    "Id": 16, 
    "LINK_Id": 16, 
    "Attachments": [ 
     { 
     "AttachmentName": "Attachment.jpeg", 
     "AttachmentLinkId": 6, 
     "LinkType": "Thread", 
     "ThreadId": 20 
     }, 
     { 
     "AttachmentName": "Attachment.txt", 
     "AttachmentLinkId": 7, 
     "LinkType": "Thread", 
     "ThreadId": 20 
     } 
    ] 
    }, 
    "16": { 
    "Id": 16, 
    "LINK_Id": 169 
    }, 
    "19": { 
    "Id": 19, 
    "LINK_Id": 112 

} }

誰が助けることができればそれは非常に高く評価されるだろう。

多くのおかげで、ORDERBY Go through thisを使用している間

答えて

0

いくつかの制限やルールがあります。 Iterating an Object of Objects (Acting as an Associative Array or Hash)このタイトルはあなたが達成しようとしているものです。

0

置き換えますと

<div ng-repeat="x in thread track by x.Id | orderBy: x.Id"> 

<div ng-repeat="x in thread | orderBy: 'Id' track by x.Id"> 
0

を、私は私の知る限り、あなたはソートする必要はありません。でもので、($の範囲内で配列をソートすることはありません動的に)、私はむしろ並べ替えメソッドを組み込みのJavascript(高速)でコントローラでソートしたいと思います。

例えば

JSONが返された後、

angular.module('sample-app', []) 
 

 
.controller('SampleController', ['$http', '$scope', function($http, $scope) { 
 

 
    $scope.test = 'Array Sort'; 
 

 
    $http.get('https://jsonplaceholder.typicode.com/posts/') 
 
    .then(function(result) { 
 
    
 
    // Order id DESC (change a and b position for ASC) 
 
    result.data.sort(function(a, b){ 
 
     return b.id - a.id 
 
    }); 
 
    
 
    // inject into scope 
 
    $scope.items = result.data; 
 
    }); 
 

 
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 

 
<div ng-app="sample-app"> 
 
    <section ng-controller="SampleController"> 
 
    <h3>{{ test }}</h3> 
 
    <ul> 
 
     <li ng-repeat="item in items"> 
 
     {{ item.id }} 
 
     </li> 
 
    </ul> 
 
    </section> 
 
</div>