简体   繁体   English

如何在范围AngularJS中获取变量的值

[英]How to get value of variable in scope AngularJS

My ng-repeat="slide in slides" and my code looks something like this: 我的ng-repeat =“幻灯片幻灯片”,我的代码看起来像这样:

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. 例如对于幻灯片1,间隔为5000,依此类推。

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

Supposing you want the slide interval in the interval variable: 假设您想要interval变量中的滑动间隔:

function nextSlide() {
    $scope.currentIndex = ($scope.currentIndex < $scope.slides.length - 1) ? ++$scope.currentIndex : 0;
    var interval = $scope.slides[$scope.currentIndex].interval;
    $timeout(nextSlide, interval);
}

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): 或者如果你想要一个只是间隔的数组,你可以使用像lodash这样的东西(注意使用ES6胖箭头 - 如果你愿意的话,可以用回调替换它):

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

With callback 随回调

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM