2017-02-17 4 views
1

Ich habe ein mehrdimensionales Array, das ich zu JSON analysiere und ich möchte in AngularJS "bestellen". und ich bin immer diese Fehlermeldung: Fehler: [orderBy: notarray] Erwartete Array aber erhalten: 13AngularJS Mehrdimensionales Array Ordnen nach

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

Hier meine JSON ist:

{ 
    "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 

} }

Wenn jemand helfen könnte es wäre sehr geschätzt.

Vielen Dank,

Antwort

0

Es gibt einige Einschränkungen und Regeln während orderBy mit Go through this. Iterating an Object of Objects (Acting as an Associative Array or Hash) Dieser Titel ist, was Sie versuchen zu erreichen.

0

ersetzen:

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

mit:

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

Ich würde nicht das Array innerhalb des $ scope sortieren (auch weil soweit ich sehen kann, brauchen Sie nicht zu sortieren es dynamisch), würde ich es lieber in der Steuerung mit der Sortiermethode eingebautem Javascript (schneller) sortieren.

Zum Beispiel nach der JSON,

zurückgegeben

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>