Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
65
rated 0 times [  70] [ 5]  / answers: 1 / hits: 52846  / 11 Years ago, thu, november 21, 2013, 12:00:00

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>

More From » arrays

 Answers
115

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.


[#74150] Wednesday, November 20, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
elishaannac

Total Points: 28
Total Questions: 97
Total Answers: 101

Location: Samoa
Member since Mon, Nov 8, 2021
3 Years ago
elishaannac questions
Sun, Dec 5, 21, 00:00, 3 Years ago
Mon, Jun 14, 21, 00:00, 3 Years ago
Mon, Jul 22, 19, 00:00, 5 Years ago
Mon, Jul 8, 19, 00:00, 5 Years ago
;