Skip to content

Instantly share code, notes, and snippets.

@jpwilliams000
Last active February 27, 2018 17:09
Show Gist options
  • Save jpwilliams000/fafa79415d0673fab9c29d00e680743b to your computer and use it in GitHub Desktop.
Save jpwilliams000/fafa79415d0673fab9c29d00e680743b to your computer and use it in GitHub Desktop.
//Angularjs 1.1 looping though an array, for use with next and previous buttons.
//Two different styles that do the same thing
//This loops through an array
$scope.nextItem = (Number($routeParams.itemId) + 1) % $scope.artists.length;
$scope.prevItem = (Number($routeParams.itemId) + $scope.artists.length - 1) % $scope.artists.length;
//This is the same thing as above
if ($routeParams.itemId > 0) {
$scope.prevItem = Number($routeParams.itemId) - 1;
} else {
$scope.prevItem = $scope.artists.length - 1;
}
if ($routeParams.itemId < $scope.artists.length-1) {
$scope.nextItem = Number($routeParams.itemId) + 1;
} else {
$scope.nextItem = 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment