使用ng-repeat和过滤器时对象在数组中的$ index [英] $index of Object in Array while using ng-repeat and a filter

查看:45
本文介绍了使用ng-repeat和过滤器时对象在数组中的$ index的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对棱角还很陌生,已经能够解决一些问题了.但我似乎找不到这种情况的答案...

Im fairly new to angular and have been able to get around somewhat. But I cant seem to find the answer to this scenario...

我有一系列对象,这些对象是我从firebase中拉下来的.我正在为对象使用ng-repeat,然后相应地显示数据.我正在尝试将索引作为routeparam传递给编辑"控制器.在这种情况下,我希望像预期的那样提取对象数据.但是,当我过滤ng-repeat时,我得到的是过滤内容的索引.找到真正的索引在哪里错了?

I have an array of objects, which I am pulling down from firebase. I am using an ng-repeat for the objects, then displaying data accordingly. I am trying to pass the index as a routeparam to an "edit" controller. In which case I would like to pull the object data as one would anticipate. However, when I filter the ng-repeat I am getting the index of the filtered content. where am I going wrong in finding the true index?

  .config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
$routeProvider
  .when('/profiles/:index', {
    templateUrl: '../views/profile.html',
    controller: 'profileCtrl'
  });

路线在上方,控制器在下方

Route is above, Controller is below

  .controller('profileCtrl', function( $scope, $routeParams ){

$scope.teamProfile = $scope.ourTeam[$routeParams.index];
    $scope.index = $routeParams.index;
});

最后是重复中的html片段.

And finally the snippet of html from within the repeat.

<div class="profileName"><a href="/profiles/{{$index}}">{{member.name}}</a><span class="handle">{{member.handle}}</span></div>

推荐答案

不幸的是, $index 是仅重复元素的迭代器偏移量(0..length-1)"

如果您想要原始索引,则必须在过滤之前将其添加到集合中,或者根本不过滤元素.

If you want the original index you would have to add that to your collection before filtering, or simply not filter the elements at all.

一种可能的方法:

angular.forEach(members, function(member, index){
   //Just add the index to your item
   member.index = index;
});

<div ng-repeat="member in members">
   <a href="/profiles/{{member.index}}">
</div>

现在,似乎这种信息实际上更像是ID ,而不是其他任何信息.希望这已成为记录的一部分,您可以绑定到该记录,而不必使用索引.

Now, it also seems that this kind of information is really more like an ID than anything else. Hopefully that is already part of the record, and you can bind to that instead of using the index.

这篇关于使用ng-repeat和过滤器时对象在数组中的$ index的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆