1

My ng-repeat="slide in slides" and my code looks something like this:

slides = [{ src: "/sikiosk/slidedata/Global/NA/USNS/Cafeteria/5000_24.jpg", interval: 5000 }, { src: "/sikiosk/slidedata/Global/NA/USNS/Cafeteria/5000_login-register2.png", interval: 15000}];

What is the best way to retrieve the intervals and store each interval as a variable? For example for slide 1, interval is 5000 and so on.

function nextSlide() {
            $scope.currentIndex = ($scope.currentIndex < $scope.slides.length - 1) ? ++$scope.currentIndex : 0;
            $timeout(nextSlide, interval);
        }
2
  • Why don't you just pass the interval to the function in your html? Or if you have the index then just get it from slides $scope.slides[$scope.currentIndex].interval Commented Feb 1, 2018 at 15:11
  • how about slides[$scope.currentIndex] ? Commented Feb 1, 2018 at 15:11

2 Answers 2

2

Supposing you want the slide interval in the interval variable:

function nextSlide() {
    $scope.currentIndex = ($scope.currentIndex < $scope.slides.length - 1) ? ++$scope.currentIndex : 0;
    var interval = $scope.slides[$scope.currentIndex].interval;
    $timeout(nextSlide, interval);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! I am new to angular, and this helps a ton. Appreciate it. I will accept this answer once it allows me to.
1

It sounds like slides is an array of objects, each of which has an interval property right? you can do:

var interval = $scope.slides[$scope.currentIndex].interval

Or if you'd rather an array of just intervals you could use something like lodash (note the use of the ES6 fat arrow -- you could replace that with a callback if you'd prefer):

var intervals = _.select($scope.slides, (x) => x.interval)

With callback

var intervals = _.select($scope.slides, function(x){return x. interval})

Comments

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.