控制器中的 ng-show 和 $index

ng-show in controller with $index

我有带有 ng-repeat 的项目列表,我需要在某些事件(如点击等)的项目旁边显示 "Status" 文本,我发现我可以使用 $index for,但不明白如何在控制器中使用它。 HTML:

<div ng-repat = 'item in items'> {{item}} <span ng-show = 'ShowItemStatus[$index]'> Status </span> </div>

我在控制器中使用 ng-show,它看起来像:

$scope.ItemStatus = false 我如何在控制器中获取索引,所以它看起来像 $scope.ItemStatus[$index] = false - 这对我不起作用。

$index 仅在 ng-repeat 中可用。它表示当前遍历的位置。 如果将 ng-repeat 与 for 循环进行比较

for(var i = 0; i < arr.length; i++)

$index 将映射到 i 值。 这意味着 ShowItemStatus 也应该是一个数组。您可以通过在控制器中遍历 items 来填充它。

正如 goreorto 已经提到的,$index 就像迭代数组时的变量 i。 因此您的状态数组可能如下所示:

 $scope.showItemStatus = [false, true, false];

这篇example可以帮到你。