1

This is probably a piece of cake for you math gurus. The prevSlide goes back fine 3-2-1-3-2... etc. Regardless of the point of entry. But the nexSlide starts out 1-2-3 and then it just 2-3-2-3...I'm sure it's my math

$scope.currentIndex = 0;
$scope.setCurrentSlideIndex = function (index) {
    $scope.currentIndex = index;
};
$scope.isCurrentSlideIndex = function (index) {
    return $scope.currentIndex === index;
};
$scope.prevSlide = function () {
    $scope.currentIndex = ($scope.currentIndex > 0) ? --$scope.currentIndex : $scope.slides.length - 1;
};
$scope.nextSlide = function () {
    $scope.currentIndex = ($scope.currentIndex < $scope.slides.length -1) ? ++$scope.currentIndex : 1;
};

Help!

2 Answers 2

1
$scope.nextSlide = function () {
    $scope.currentIndex = ($scope.currentIndex < $scope.slides.length -1) ? ++$scope.currentIndex : 0;
};

You just need to change the 1 to a 0 at the end of this line. index 1 as you have it now is actually the 2nd slide.

Slide #: 1 -- 2 -- 3
Index #: 0 -- 1 -- 2
Sign up to request clarification or add additional context in comments.

1 Comment

Golden! Thanks a lot!
1

You'll have to change to match your context but the logic in the functions previous() and next() below might help.

var numSlides = 3;

function previous(current){
  current--;
  return current >= 0 ? current : numSlides;
}

function next(current){
  current++;
  return current <= numSlides ? current : 0;
}

2 Comments

You can always use the modulo (%) operator for any clock arithmetic. return (++current) % numSlides
This is a good solution for pure javascript, but the other answer helped me better since it's still angular and didn't require a lot to make it work. Besides, I can't hardcode the amount of slides. It must remain dynamic i case the user adds more than 3 slides to the component. But I did +1 your answer. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.