2015-04-05 142 views
1

我無法找到一種方式來循環瀏覽json對象數組。以ng-repeat角度循環遍歷一個對象數組

我的陣列看起來像在螢火(每個系列的對象具有的索引值。):

[[0], Object { idliste=0, id=1, photo="pot.jpg", plus...}, Object { idliste=0, id=3, photo="pot.jpg", plus...}] 

[[1], Object { idliste=1, id=1, photo="pot.jpg", plus...}, Object { idliste=1, id=3, photo="pot.jpg", plus...}] 

[[2], Object { idliste=2, id=1, photo="pot.jpg", plus...}, Object { idliste=2, id=3, photo="pot.jpg", plus...}] 

已經由此代碼生成:

 var idListe = $scope.getIdListe(); 
     $scope.listeCommandes[idListe]= new Array([idListe]); 

     for (i=0;i<$scope.panier.length;i++){ 

         $scope.listeCommandes[idListe].push({ 
          idliste:idListe, 
          id:  $scope.panier[i].id, 
          photo: $scope.panier[i].photo, 
          nom: $scope.panier[i].nom, 
          quantite:$scope.panier[i].quantite, 
          prix: $scope.panier[i].prix, 
          heureajout:$scope.getHeure() 
         }); 

        }; 

$scope.getIdListe = function(){ 
     var idListe = $scope.listeCommandes.length; 
     return idListe; 
    }; 

和HTML是:

<div ng-repeat="idliste in listeCommandes" > 
     <div ng-repeat="(nom, id) in idliste"> 
      {{nom}} : {{id}} 
     </div> 
</div> 

它不起作用。我真的不知道如何使它工作,這意味着,只需按照索引號打印每個對象即可。

謝謝你,如果你有任何想法。我搜索了論壇,但找不到任何解決方案。也許這是我創建一個錯誤的索引的方式?

+0

能否請您創建一個plukr /小提琴,它真的很難通過觀察代碼 – 2015-04-05 10:32:31

+0

好理解!謝謝。 – 2015-04-05 10:39:57

回答

1

最後加入所謂的角過濾「這個例子中mentionned角模塊回答我的問題:

http://jsbin.com/weyov/45/edit?html,js,output

感謝很多關於你幫

現在我的代碼看起來象是:

var idListe = $scope.getIdListe(); 

    for (i=0;i<$scope.panier.length;i++){ 

        $scope.listeCommandes.push({ 
         idliste:idListe, 
         id:  $scope.panier[i].id, 
         photo: $scope.panier[i].photo, 
         nom: $scope.panier[i].nom, 
         quantite:$scope.panier[i].quantite, 
         prix: $scope.panier[i].prix, 
         heureajout:$scope.getHeure() 
        }); 

       }; 

和HTML:

<ul ng-repeat="(key, value) in listeCommandes | groupBy: 'idliste'"> 
    Group name: {{ key }} 
    <li ng-repeat="article in value"> 
    article: {{ article.nom}} 
    </li> 
</ul> 

然後我的訂單現在都automtically通過ID在HTML視圖分組:

Group name: 0 
article: Pots Gris 
article: Pot Vert 

Group name: 2 
article: Pot Bleu 
article: Pot Vert 
article: Pinceaux 

Group name: 5 
article: Pots Gris 
article: Pot Vert 
article: Pot Rouge 
article: Pot Bleu