我需要在 ng-click 上从 ng-repeat 中获取当前对象,我不能使用 $index,因为我使用的是 orderBy,因此它给出了相对于范围对象的错误索引。理想情况下,我希望能够单击对象(缩略图)并让 $scope.activePerson 获得所有对象值。
我的数据结构如下:
[
{
'name': 'john'
},
{
'name': 'toby'
},
{
'name': 'sarah'
}
]
这非常简单,我的真实对象有 30 多个 KV 对和子对象。我要重复 10 个对象(以 4 个为一组)。
我当前的 HTML 是:
.item.fourth(ng-repeat="person in people | orderBy:'-name' " ng-show="$index <= 3" nid="{{person.guid}}"
谢谢
最佳答案
ng-repeat="person in people"
中的person
;
我不确定你使用的是哪种 Markdown ,你肯定没有 html,但你想要这样的东西:
<div
ng-repeat="person in people | orderBy:'-name' "
ng-show="$index <= 3"
nid="{{person.guid}}"
ng-click="activePerson = person">
</div>
请注意,ng-repeat 会创建一个子作用域,因此您需要在父作用域中设置 activePerson
。
关于javascript - Angular - 获取 ng-repeat 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31528672/