ermalsh
ermalsh

Reputation: 311

How to get value of variable in scope AngularJS

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);
        }

Upvotes: 1

Views: 1003

Answers (2)

istrupin
istrupin

Reputation: 1533

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})

Upvotes: 1

Javier Gonzalez
Javier Gonzalez

Reputation: 1015

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);
}

Upvotes: 2

Related Questions