I'm doing a slider following the steps from this tutorial using Angular.js and Firebase API.
I have created a directive 'slider' in my directive file, here is the code:
.directive('slider', function()
{
return {
restrict: 'E',
replace: true,
link: function(scope, elem, attrs) {
scope.currentIndex = 0; // Initially the index is at the first slide
scope.next = function()
{
scope.currentIndex < scope.challenges.length - 1 ? scope.currentIndex++ : scope.currentIndex = 0;
};
scope.prev = function()
{
scope.currentIndex > 0 ? scope.currentIndex-- : scope.currentIndex = scope.challenges.length - 1;
};
scope.$watch('currentIndex', function()
{
scope.challenges.forEach(function(challenge)
{
challenge.visible = false; // make every challenge invisible
});
scope.challenges[scope.currentIndex].visible = true; // make the current challenge visible
});
},
template: '<div></div>'
};
I have no isolate scope, as you see, so it should take scope.challenges from parent, but it comes as undefined value instead of the data from Firebase.
So doing forEach of undefined it throws an error: TypeError: Cannot read property 'forEach' of undefined
I spent like 3 hour trying to understand scopes, a solution will be so rewarding. Thank you very much.