Angularjs 为什么我得到的是一张空照片?

Angularjs 为什么我得到的是一张空照片?,angularjs,Angularjs,我有一个小画廊。 因此,有一个SRC映像数组:$scope.link.images和函数: $scope.toogleImage = function (){ if($scope.link.images.length > $scope.index){ ++$scope.index; } else { $scope.index = 0;

我有一个小画廊。 因此,有一个SRC映像数组:
$scope.link.images
和函数:

$scope.toogleImage = function (){
                if($scope.link.images.length > $scope.index){
                    ++$scope.index;
                } else {
                    $scope.index = 0;
                }
                $scope.mainImage = $scope.link.images[$scope.index];
            }
数组中有六个元素。
当我调用方法
$scope.toogleImage
时,它递增
$scope.index
并从数组中获取元素。所以,当
$scope.index=6
时,我从数组中得到一个空的
SRC
。为什么?

如果只有六个元素,那么在基于零的数组中[6]什么都不是,因为有效值只有0,1,2,3,4,5

您可以按以下方式重写函数:

$scope.toogleImage = function (){
     $scope.mainImage = $scope.link.images[++$scope.index % $scope.link.images.length];
}

似乎没有对数组的长度进行范围检查,您的增量超过了尾数。如果数组有六个元素,索引=6处没有元素。请更具体地说明什么不起作用。索引更改正确,可能您有不同的问题。什么是运算符
%
?我的滑块不工作,因此图像不会更改。这是运算符。请看“余数”部分。