When I filter ng-repeat, this.$index and key both change to reflect the objects position in the filtered array.
当我过滤ng-repeat时,此。$ index和键都会更改以反映过滤后数组中的对象位置。
The problem is that I only see the unfiltered array from the JavaScript so this.$index and key are giving an unexpected result.
问题是我只看到JavaScript中的未过滤数组,所以这个。$ index和key给出了意想不到的结果。
HTML:
<section ng-repeat="( key, place ) in places | filter: ({ name : placeName })" >
<button ng-click='myFunc( this.$index )'>Pick {{place.name}}</button> <!-- same issue with key -->
<section>
Javascript:
myFunc(key){
console.log( $scope.places[key] );
}
I can loop over the array and find $id or my own identifier but that seems wasteful when all I need is an index that I can pass on to my function which is looking at the unfiltered array.
我可以循环遍历数组并找到$ id或我自己的标识符,但是当我需要的是一个索引时,我可以将其传递给我的函数,该函数正在查看未过滤的数组。
Thanks.
1 个解决方案
#1
2
Instead of passing in the index, pass in the actual object:
传入实际对象,而不是传入索引:
<section ng-repeat="( key, place ) in places | filter: ({ name : placeName })" >
<button ng-click='myFunc( place )'>Pick {{place.name}}</button>
<!-- same issue with key -->
<section>
Then in your JavaScript
然后在你的JavaScript中
$scope.myFunc(place){
console.log( place );
}
This will make testing the myFunc method even easier since you are supplying the object in which you will be acting upon.
这将使得测试myFunc方法变得更加容易,因为您提供了将要处理的对象。
#1
2
Instead of passing in the index, pass in the actual object:
传入实际对象,而不是传入索引:
<section ng-repeat="( key, place ) in places | filter: ({ name : placeName })" >
<button ng-click='myFunc( place )'>Pick {{place.name}}</button>
<!-- same issue with key -->
<section>
Then in your JavaScript
然后在你的JavaScript中
$scope.myFunc(place){
console.log( place );
}
This will make testing the myFunc method even easier since you are supplying the object in which you will be acting upon.
这将使得测试myFunc方法变得更加容易,因为您提供了将要处理的对象。