簡體   English   中英

AngularJS:ng-repeat,過濾器和索引

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

對不起,我的語言能力,希望您能理解。

我對ng-repeat和過濾器數據有疑問。 我使用$ index從ng-repeat數據中獲取一些數據。 例如,我有20個元素。 當我單擊第三個元素時,索引為3,應用程序向我顯示有關第三個元素的更多數據。 現在,當我使用過濾器時,它無法正常工作,因為當我過濾最后一個元素並單擊它時,索引為0,應用程序向我顯示了有關第一個元素的更多數據。 我想做的是,例如,當我過濾第15個元素時,當我單擊它時,我會得到有關第15個元素的更多數據,而不是第一個。

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腳本:

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

為什么使用$ index 您可以只使用迭代項

采用

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

不與$ index

addRestaurant函數中,您已經單擊了餐廳

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM