2015-04-05 79 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