AngularJS - 如何从控制器中的 ng-repeat 访问下一项 [英] AngularJS - How to access to the next item from a ng-repeat in the controller

查看:18
本文介绍了AngularJS - 如何从控制器中的 ng-repeat 访问下一项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在单击按钮时访问屏​​幕上下一项的参数.

I'd like to access to the parameters of the next item on screen when clicking on a button.

我在我的 html 文件中使用了 ng-repeat:

I use a ng-repeat in my html file:

<li ng-repeat="item in items | filter:query" ng-show="isSelected($index)">
    <a href="" ng-click="itemNext()"><img src="xxx.jpg" /></a>
</li>

以及我的控制器中带有循环的索引:

And the index in my Controller with a loop:

$scope.itemNext = function () {
    $scope._Index = ($scope._Index < $scope.nbItems - 1) ? ++$scope._Index : 0;
    $scope.functionToCallWithNextItem(nextItem.param1);
};

一个简单的 $scope.items[$scope._Index].param1 而不是 nextItem.param1 将不起作用,因为数据被过滤,所以 $$scope.items 中的 index+1 不一定是好的.

A simple $scope.items[$scope._Index].param1 instead of nextItem.param1 wouldn't work as the data is filtered so $index+1 from $scope.items isn't necessarily the good one.

有什么想法吗?

推荐答案

您可以将过滤后的数据分配给变量:

You can assign your filtered data to a variable:

<li ng-repeat="item in (filteredItems = (items | filter:query))">

然后使用$index + 1得到下一项:

<a ng-click="itemNext(filteredItems[$index + 1])">

演示: http://plnkr.co/edit/OdL5rIxtTEHnQCC3g4LS?p=预览

这篇关于AngularJS - 如何从控制器中的 ng-repeat 访问下一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆