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

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

问题描述

我对 angular 还很陌生,并且已经能够有所了解.但我似乎无法找到这种情况的答案......

我有一组对象,我正在从 firebase 中提取这些对象.我对对象使用 ng-repeat,然后相应地显示数据.我试图将索引作为路由参数传递给编辑"控制器.在这种情况下,我想按照预期提取对象数据.但是,当我过滤 ng-repeat 时,我得到了过滤内容的索引.我在寻找真正的索引时哪里出错了?

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

路由在上面,控制器在下面

 .controller('profileCtrl', function( $scope, $routeParams ){$scope.teamProfile = $scope.ourTeam[$routeParams.index];$scope.index = $routeParams.index;});

最后是重复中的 html 片段.

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

解决方案

很遗憾 $index 只是重复元素的迭代器偏移量(0..length-1)"

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

一种可能的方法:

angular.forEach(members, function(member, index){//只需将索引添加到您的项目member.index = 索引;});<div ng-repeat="member in members"><a href="/profiles/{{member.index}}">

现在看来,这种信息确实更像是一个 ID,而不是其他任何东西.希望这已经是记录的一部分,您可以绑定到该记录而不是使用索引.

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

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;
});

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>

解决方案

Unfortunately $index is only the "iterator offset of the repeated element (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.

One possible approach:

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>

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天全站免登陆