Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 角度,通过数组单击_Javascript_Angularjs - Fatal编程技术网

Javascript 角度,通过数组单击

Javascript 角度,通过数组单击,javascript,angularjs,Javascript,Angularjs,我有一个设置为数组索引的变量,我想在单击时尝试循环它。我最初将其设置为: $scope.calenderState = ["day","week","month","year"]; $scope.mod2m4 = $scope.calenderState[0]; 还有一个连接的点击功能,发送mod2m4如下: ng-click ="changeCal(mod2m4)" 所以现在它会发送“day”。每次我点击它,我希望它增加到下一个项目,(所以一周),除非它的年份,然后返回到一天。我如

我有一个设置为数组索引的变量,我想在单击时尝试循环它。我最初将其设置为:

 $scope.calenderState = ["day","week","month","year"];
 $scope.mod2m4 = $scope.calenderState[0];
还有一个连接的点击功能,发送
mod2m4
如下:

 ng-click ="changeCal(mod2m4)" 
所以现在它会发送“day”。每次我点击它,我希望它增加到下一个项目,(所以一周),除非它的年份,然后返回到一天。我如何处理这种情况

编辑: 我可以这样解决它,但我想知道是否有更优雅的解决方案

var ind = _.indexOf($scope.calenderState, current);
if(ind == 3){
    $scope.mod2m4 = $scope.calenderState[0];
}else{
    $scope.mod2m4 = $scope.calenderState[ind + 1];
}

也许可以尝试将索引存储为范围变量,然后将增量操作抽象为函数(以处理换行)

然后,在视图中,如果要显示值,可以使用以下表达式:

<p>{{calendarState[index]}}</p>
{{calendarState[index]}


可以尝试将索引存储为范围变量,然后将增量操作抽象为函数(以处理换行)

然后,在视图中,如果要显示值,可以使用以下表达式:

<p>{{calendarState[index]}}</p>
{{calendarState[index]}


如果您不介意数组的顺序:

$scope.calenderState.push($scope.calenderState.shift());
$scope.mod2m4 = $scope.calenderState[0];

如果您不介意数组的顺序:

$scope.calenderState.push($scope.calenderState.shift());
$scope.mod2m4 = $scope.calenderState[0];