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>
You can use unique filter while using ng-repeat . If you use track by $index then unique won't work.
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.
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.
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.
Try this :
<div ng-repeat="member in members">
{{members.indexOf(member)}}
</div>
indexOf always returns the original index in an ng-repeat
Demo
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With