JavaScript – 使用ng-repeat和过滤器的Array中的Object的$index

前端之家收集整理的这篇文章主要介绍了JavaScript – 使用ng-repeat和过滤器的Array中的Object的$index前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我相当新的有角度,已经能够绕过一些.但是我似乎找不到这个场景的答案…

我有一个数组的对象,我正在从firebase拉下来.我正在对象使用ng重复,然后相应地显示数据.我试图将索引作为一个routeparam传递给“编辑”控制器.在这种情况下,我想按照预期的方式提取对象数据.但是,当我过滤ng-repeat时,我得到过滤内容的索引.找到真正的索引我在哪里错了?

.config(['$routeProvider','$locationProvider',function($routeProvider,$locationProvider) {
$routeProvider
  .when('/profiles/:index',{
    templateUrl: '../views/profile.html',controller: '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="handle">{{member.handle}}</span></div>

解决方法

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

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

一种可能的方法

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.希望这已经是记录的一部分,你可以绑定到而不是使用索引.

原文链接:https://www.f2er.com/js/151831.html

猜你在找的JavaScript相关文章