简体   繁体   English

AngularJS:如何将ngRepeat指令与数组和子数组一起使用?

[英]AngularJS: How to use the ngRepeat directive together with arrays and subarrays?

Let's assume there is an array of categories. 假设存在一系列类别。 Contained within this array, for each category, there is a variable number of images. 对于每个类别,此数组中包含的图像数量是可变的。 Additionally, each image has a sub-array of paths for varying sizes of each image. 另外,每个图像都有一个路径子数组,用于改变每个图像的大小。 Listed below is an example. 下面列出的是一个示例。

$scope.categories = [
  { "id": 1, "name": "Fashion", "images": [
    { "id": 1, "paths": [
      { "id": 1, "pathThumb":"thumbnail_A.jpg"},
      { "id": 2, "pathFull":"full_size_A.jpg"}
    ]}
  ]},
  { "id": 2, "name": "Sunsets", "images": [
    { "id": 1, "paths": [
      { "id": 1, "pathThumb":"thumbnail_B.jpg"}
      { "id": 2, "pathFull":"full_size_B.jpg"}
    ]},
    { "id": 2, "paths": [
      { "id": 1, "pathThumb":"thumbnail_C.jpg"}
      { "id": 2, "pathFull":"full_size_C.jpg"}
    ]}
  ]}
];

I would like to use the AngularJS ngRepeat directive to loop through the above data to produce the following 我想使用AngularJS ngRepeat指令遍历上述数据以产生以下内容

<div class="item fashion">
  <a href="full_size_A.jpg"><img src="thumbnail_A.jpg"></a>
</div>
<div class="item sunset">
  <a href="full_size_B.jpg"><img src="thumbnail_B.jpg"></a>
</div>
<div class="item sunset">
  <a href="full_size_C.jpg"><img src="thumbnail_C.jpg"></a>
</div>

I assume it would look something like the following: 我认为它看起来像以下内容:

<div class="item {{category.name}}" ng-repeat="...">
  <a href="{{category.image.path.pathFull}}"><img src="{{category.image.path.pathThumb}}"></a>
</div>

I am not sure how to loop through an array with sub-array and extract the appropriate data. 我不确定如何使用子数组遍历数组并提取适当的数据。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Use like: 使用方式如下:

<div ng-repeat="item in categories">
    <div ng-repeat="image in item.images">
        <a ng-href="{{image.paths[1].pathFull}}">
            <img ng-src="{{image.paths[0].pathThumb}}">
        </a>
    </div>
</div>

you should use a wrapper container in order to get subarray of categories.. 您应该使用包装容器以获取类别的子数组。

HTML 的HTML

  <div ng-repeat="category in categories">
    <div class="item {{category.name | lowercase}}" ng-repeat="image in category.images">
      <a ng-href="{{image.paths[1].path}}">
        <img ng-src="{{image.paths[0].path}}">
      </a>
    </div>
  </div>

here I used dynamic class with ng-class and using lowercase filter to get lowercase version of name string as class... 这里我将动态类ng-class使用,并使用小写过滤器将名称字符串的小写版本作为类...

using ng-src as src attribute of img is important here because sometimes given interpolate ( {{something}} ) cause fail to load resource, because of data is not fecthed... 采用ng-srcsrc属性img是这里重要的,因为有时给插值( {{something}}原因无法加载资源,因为数据不fecthed ...

same thing for ng-href... ng-href也是一样...

here is PLUNKER ... 这是PLUNKER ...

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

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