如何在ng-repeat中按索引查找?

时间:2013-08-28 16:29:52

标签: angularjs angularjs-directive

在我看来,我有:

<div class="grid-wrapper grid-view type-A has-thumbnail" ng-repeat="artist in artists">

因此artist.id有该艺术家的ID。

我有另一个名为album_artists的对象,它看起来像:

album_artists = {
  '1231': {
    ...
  },
  '1232': {
    ...
  }
}

所以在我的div, I want to get the album_artists that corresponds to a particular artist.id`中。我怎么能这样做?

我想要的是:

<div class="grid-wrapper grid-view type-A has-thumbnail" ng-repeat="artist in artists">
  <div class="otherstuff" ng-repeat="album in albums[artist.id]">

这有效吗?

1 个答案:

答案 0 :(得分:1)

如果你想做

<div class="otherstuff" ng-repeat="album in album_artists[artist.id]">

然后你必须将你的数据结构更改为数组的每个键值(例如'1231'),就像这样

album_artists = {
    '1231': [{...
    }],
    '1232': [{...
    }]
}

如果您不想更改数据结构,可以执行

<div class="otherstuff" ng-repeat="(key, value) in album_artists[artist.id]">