Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$index of Object in Array while using ng-repeat and a filter

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>
like image 400
lutonmedia Avatar asked Nov 21 '13 03:11

lutonmedia


People also ask

How do you prevent duplicates in NG-repeat?

You can use unique filter while using ng-repeat . If you use track by $index then unique won't work.

How do you filter NG-repeat?

The ng-repeat values can be filtered according to the ng-model in AngularJS by using the value of the input field as an expression in a filter. We can set the ng-model directive on an input field to filter ng-repeat values.

How do you condition in NG-repeat?

You can add condition using ng-if also and this is effective too. You can apply any condition to your list using ng-if attribute. In below example, I have put condition where Age > 20 and IsActive is true. ng-repeat will fetch all records which full fill this scenario.

How do I get the index of an element in NG-repeat?

Note: The $index variable is used to get the Index of the Row created by ng-repeat directive. Each row of the HTML Table consists of a Button which has been assigned ng-click directive. The $index variable is passed as parameter to the GetRowIndex function.


2 Answers

Try this :

<div ng-repeat="member in members">
{{members.indexOf(member)}}
</div>

indexOf always returns the original index in an ng-repeat

Demo

like image 122
Manas Avatar answered Oct 25 '22 01:10

Manas


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.

like image 42
Josh Avatar answered Oct 24 '22 23:10

Josh