Last active
March 1, 2022 08:59
-
-
Save eimg/6829747 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var app = angular.module('ngSlides', []); | |
app.controller("SlideController", function ($scope) { | |
$scope.slides = []; | |
$scope.activeSlide = 0; | |
$scope.addSlide = function() { | |
if(!$scope.slideTitle && !$scope.slideBody) { | |
return false; | |
} | |
$scope.hideAll(); | |
$scope.activeSlide = $scope.slides.length; | |
$scope.slides.push({ | |
title: $scope.slideTitle, | |
body: $scope.slideBody, | |
state: 'show' | |
}); | |
$scope.slideTitle = ''; | |
$scope.slideBody = ''; | |
}; | |
$scope.removeSlide = function(slide) { | |
var index = $scope.slides.indexOf(slide); | |
$scope.slides.splice(index, 1); | |
$scope.next(); | |
}; | |
$scope.hideAll = function() { | |
angular.forEach($scope.slides, function(slide) { | |
slide.state = 'hide'; | |
}); | |
}; | |
$scope.next = function() { | |
var next = $scope.activeSlide + 1; | |
if(next >= $scope.slides.length) next = 0; | |
$scope.activeSlide = next; | |
$scope.hideAll(); | |
$scope.slides[next].state = 'show'; | |
}; | |
$scope.previous = function() { | |
var prev = $scope.activeSlide - 1; | |
if(prev < 0) prev = $scope.slides.length - 1; | |
$scope.activeSlide = prev; | |
$scope.hideAll(); | |
$scope.slides[prev].state = 'show'; | |
}; | |
}); | |
app.directive("next", function() { | |
return { | |
restrict: "E", | |
template: '<a href="#" class="next">Next »</a>', | |
link: function(scope, element) { | |
element.bind('click', function() { | |
scope.$apply("next()"); | |
}); | |
} | |
} | |
}); | |
app.directive("previous", function() { | |
return { | |
restrict: "E", | |
template: '<a href="#" class="previous">« Previous</a>', | |
link: function(scope, element) { | |
element.bind('click', function() { | |
scope.$apply("previous()"); | |
}); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment