简体   繁体   English

如何在Angular JS中重复显示json数据?

[英]How to alternatively repeat json data in Angular JS?

I want to alternatively repeat the json data. 我想替代地重复json数据。 I've data like below: 我有如下数据:

JSON: JSON:

  {
       "birds":{
          "birdInfo":[
             {
                "name":"parrot",
                "color":"green"
             },
             {
                "name":"peacock",
                "color":"green"
             }
          ]
       },
       "animals":{
          "animalInfo":[
             {
                "name":"lion",
                "designation":"king"
             },
             {
                "name":"lioness",
                "designation":"queen"
             }
          ]
       }
    }

In my controller: 在我的控制器中:

    $scope.birdInfo = response.birds.birdInfo;
    $scope.animalInfo = response.animals.animalInfo;

In my View: 在我看来:

 <div ng-repeat="bird in birdInfo">
   {{bird.name}}
 </div>

 <div ng-repeat="animal in animalInfo">
   {{animal.name}}
 </div>

What I want: 我想要的是:

I want data to be display like: 我希望数据显示为:

Scenario 1 : one alternative repeat item 方案1:一个替代重复项

  parrot
  LION
  peacock
  LIONESS

Scenario 1 : two alternative repeat items 方案1:两个替代重复项

  parrot
  peacock
  LION
  LIONESS
  crow
  dove
  TIGER
  ELEPHANT

How to do that in angular? 如何做到这一点?

Here is the plnkr: http://plnkr.co/edit/hN7odUF3MLJe4Ad9IBNM?p=preview 这是plnkr: http ://plnkr.co/edit/hN7odUF3MLJe4Ad9IBNM?p=preview

You could make a function to merge your 2 arrays into a 3rd array with the order you need, and then just iterate over that in html. 您可以创建一个函数,将2个数组按所需顺序合并到第3个数组中,然后在html中对其进行迭代。

It would look something like this, if an array is longer you can append the items at the end: 看起来像这样,如果数组更长,则可以在末尾追加项目:

  function merge(first, second) { var merged = []; var shortest = Math.min(first.length, second.length); for (var i = 0; i < shortest; i++) { merged.push(first[i]); merged.push(second[i]); } if (first.length != second.length) { var left = first.length > second.length ? first.slice(second.length) : second.slice(first.length); console.log(left); merged = merged.concat(left); } return merged; } 

Check this plunker to see it working. 检查此扬声器 ,看看它是否有效。

You can concat the two arrays inside ng-repeat as below 您可以在ng-repeat中合并两个数组,如下所示

ng-repeat="birdOrAnimal in concatenatedData = (birdInfo.concat(animalInfo))"

I've just updated your plunker with this. 我刚刚用这个更新了你的朋克。

http://plnkr.co/edit/uOj2XrgYKzLXpurzp8Mv http://plnkr.co/edit/uOj2XrgYKzLXpurzp8Mv

Here is another approach: 这是另一种方法:

http://plnkr.co/edit/bV0Bd6LINmzcjQodC9Cj?p=preview http://plnkr.co/edit/bV0Bd6LINmzcjQodC9Cj?p=preview

In this approach, I'm creating a repeater that just uses the $index value to count through the objects: 在这种方法中,我创建了一个仅使用$ index值对对象进行计数的转发器:

<div ng-repeat="i in getNumber(number) track by $index">
    <p>{{birdInfo[$index].name}} - {{birdInfo[$index].color}}</p>
    <p>{{animalInfo[$index].name}} - {{animalInfo[$index].designation}}</p>
</div>

The length is counted by the bird length. 长度由鸟的长度计算。

$scope.number = $scope.birdInfo.length;
      $scope.getNumber = function() {
      return new Array($scope.number);
}

Try this: 尝试这个:

`<div ng-repeat="bird in birdInfo" ng-if="birdInfo.length>=animalInfo.length">
   <div>{{bird.name}} - {{bird.color}}</div>
   <div>{{animalInfo[$index].name}} - {{animalInfo[$index].designation}}</div>
 </div>
 <div ng-repeat="ani in animalInfo" ng-if="birdInfo.length<animalInfo.length">
   <div>{{birdInfo[$index].name}} - {{birdInfo[$index].color}}</div>
   <div>{{ani.name}} - {{ani.designation}}</div>
 </div>`

see this plunker 看到这个矮人

It's more about algorithms than AngularJS. 它比AngularJS更重要的是算法。

You can Array concat() your arrays birdInfo and animalInfo to loop through all your Objects literal having a "name" property. 您可以对concat()数组进行数组birdInfo和animalInfo遍历所有具有“ name”属性的Object常量。 Then displaying "one alternative" or "two alternative" (sic) is just about playing with array Indexes. 然后显示“一个替代”或“两个替代”(原文如此)仅与数组索引一起玩。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM