简体   繁体   English

AngularJS:ng-repeat,过滤器和索引

[英]AngularJS: ng-repeat, filter and index

Sorry for my language skills, hope You understand all. 对不起,我的语言能力,希望您能理解。

I have a problem with ng-repeat and filter data. 我对ng-repeat和过滤器数据有疑问。 Im using $index, to get some data from ng-repeat data. 我使用$ index从ng-repeat数据中获取一些数据。 For example, i have 20 elements. 例如,我有20个元素。 when i click on third, then index is 3, and application display me more data about third element. 当我单击第三个元素时,索引为3,应用程序向我显示有关第三个元素的更多数据。 Now, when i use filter, it doesnt work well, becouse when i filter last element, and click on it, index is 0 and application show me more data about first element. 现在,当我使用过滤器时,它无法正常工作,因为当我过滤最后一个元素并单击它时,索引为0,应用程序向我显示了有关第一个元素的更多数据。 What i want to do, is when i filter,for example, 15th element and when i click on it, i get more data about 15th element, not first. 我想做的是,例如,当我过滤第15个元素时,当我单击它时,我会得到有关第15个元素的更多数据,而不是第一个。

HTML: HTML:

.panel-body ng-repeat=("restaurant in restaurants | filter:search") ng-click="addRestaurant($index);"
                a aria-expanded="true" data-parent="#accordion" data-toggle="collapse" style="font-size: 18px;" 
                  i.fa.fa-chevron-right.pull-right.text-muted ng-show="$index == index"
                  | {{restaurant.name}}

JS SCRIPT: JS脚本:

$scope.addRestaurant = (index) ->
  $scope.index = index
  $scope.selectedRestaurant = $scope.restaurants[$scope.index].id

Why are you using $index ? 为什么使用$ index You can use just the iteration item 您可以只使用迭代项

Use 采用

$scope.addRestaurant = (restaurant)...
...
ng-repeat=("restaurant in restaurants | filter:search") ng-click="addRestaurant(restaurant);"

not with $index 不与$ index

in addRestaurant function you already have clicked restaurant addRestaurant函数中,您已经单击了餐厅

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM