Angular -获取ng-repeat对象
Angular - Get ng-repeat object
我需要在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
;
我不知道你在用什么标记,你肯定没有html,但你想要这样的:
<div
ng-repeat="person in people | orderBy:'-name' "
ng-show="$index <= 3"
nid="{{person.guid}}"
ng-click="activePerson = person">
</div>
注意ng-repeat创建了一个子作用域,因此您需要在父作用域中已经设置了activePerson
。
您可以使用orderBy并从ng-repeat复制当前对象,参见此plunkr。相关代码:
控制器var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.stuff = [
{
'name': 'john'
},
{
'name': 'toby'
},
{
'name': 'sarah'
}
];
$scope.setActivePerson = function (obj) {
$scope.activePerson = obj;
};
});
<<p> 视图/strong> <body ng-controller="MainCtrl">
<div ng-repeat="thing in stuff | orderBy: 'name'">
<input type="radio" ng-click="setActivePerson(thing)" />
{{ thing.name }}
</div>
<br />
<div ng-model="activePerson">Active: {{ activePerson.name }}</div>
</body>
相关文章:
- 如何使用ng repeat中的选定输入更新我的对象
- 无法使用ng repeat检索动态创建的JSON对象的属性
- AngularJS:根据其他对象预先选择ng repeat中的select元素
- 角度控制器不绑定服务对象属性 ng-repeat
- 如何将 JSON 对象映射到 ng-repeat 中的折叠行
- 在angularjs ng-repeat指令中对对象进行排序
- ng-repeat不绑定ng-model对象数组
- 使用 ng-repeat解析不均匀数据/ json对象
- 使用Angular js ng repeat指令过滤json对象
- 如何将ng repeat中的当前对象传递给$mdDialog
- I'我正在努力学习Angular——为什么不;t我的对象没有用ng repeat出现
- 如何在ng repeat中比较两个不同的对象
- 使用ng repeat将表单元素绑定到角度中的新对象
- 角度在ng-repeat(MEAN-Stack)中传递错误的对象
- AngularJS ng-repeat对象,按对象过滤
- Angular JS嵌套的ng-repeat只有一个顶级对象
- 如何在 ng-repeat 中按键分配对象数组
- ng-repeat中的Angular Js对象过滤器
- Angular 什么是在 ng-repeat 指令中访问总对象的最佳方式
- AngularJS - ng-repeat,更新尚未定义的模型对象索引