javascript - How to get number of images in a slider and passing to next/previous function in AngularJS? -


i found tutorial on how build basic image slider in angularjs. have working great in own solution have come across 1 issue not sure how solve.

in tutorial, image sources being hardcoded $scope using $scope.slides defined in app.js. unfortunately, approach won't work me because going have multiple sliders populated dynamically based on json data.

the issue running next/previous functions of slider rely on getting count of total number of images within slider. in example using $scope.slides.length accomplish this. if hardcode number of images in slider i've built, works perfectly, know how can generate number dynamically depending on slides being populated.

here's code app.js:

$scope.direction = 'left'; $scope.currentindex = 0;  $scope.setcurrentslideindex = function (index) {     $scope.direction = (index > $scope.currentindex) ? 'left' : 'right';     $scope.currentindex = index; };  $scope.iscurrentslideindex = function (index) {     return $scope.currentindex === index; };  $scope.prevslide = function () {     $scope.direction = 'left';     $scope.currentindex = ($scope.currentindex < $scope.slides.length - 1) ? ++$scope.currentindex : 0;     //$scope.slides.length should dynamic };  $scope.nextslide = function () {     $scope.direction = 'right';     $scope.currentindex = ($scope.currentindex > 0) ? --$scope.currentindex : $scope.slides.length - 1;     //$scope.slides.length should dynamic }; 

here's slider template:

<div class="slider">     <img ng-repeat="slide in vm.selectedproduct.images" class="slide slide-animation nondraggableimage" ng-swipe-right="nextslide()" ng-swipe-left="prevslide()" ng-hide="!iscurrentslideindex($index)" ng-src="{{slide.source}}">     <a class="arrow prev" href="#" ng-click="nextslide()"></a>     <a class="arrow next" href="#" ng-click="prevslide()"></a>     <nav class="nav">         <div class="wrapper">             <ul class="dots">                 <li class="dot" ng-repeat="slide in slides">                     <a href="#" ng-class="{'active':iscurrentslideindex($index)}" ng-click="setcurrentslideindex($index);"></a>                 </li>             </ul>         </div>     </nav> </div> 

here's working example of slider using hardcoded values $scope.slides.length (click button , click link see it)

plunker

never mind, solution simple didn't occur me. vm.selectedproduct.images.length needed. didn't think work within app.js due scope, worked perfectly!


Comments

Popular posts from this blog

javascript - generate date range base on integers -

PHP and MySQL WP -