I'm new to this directive in AngularJS and would need some help to get this javascript function to work. It was okay when I just used plain JavaScript outside the angular directive. But now I have hit some problems.
It's okay down to the function navigate
and I get an error that:
removeClass is not a function
It works with addClass
so I don't understand why remove doesn't work here.
<ol class="content">
<li>
"{{ myData.quotes[0].quote }}"
<br /> <div class="quoteLine"></div> <span class="boldText"> {{ myData.quotes[0].person }} </span>
</li>
<li>
"{{ myData.quotes[1].quote }}"
<br />
<div class="quoteLine"></div> <span class="boldText"> {{ myData.quotes[1].person }} </span>
</li>
</ol>
app.directive('myCarousel', function () {
return {
link: function ($scope, $element) {
var next = $element.find('.next');
var prev = $element.find('.prev');
var items = $element.find('.content li');
var counter = 0;
var amount = items.length;
var current = items[0];
$element.addClass('active')
function navigate (direction) {
current.removeClass('current')
counter = counter + direction
if (direction === -1 && counter < 0) {
counter = amount - 1
}
if (direction === 1 && !items[counter]) {
counter = 0
}
current = items[counter]
current.addClass('current')
}
// go to next quote
next.on('click', function (ev) {
navigate(1)
})
// go back to previous quote
prev.on('click', function (ev) {
navigate(-1)
})
navigate(0)
}
}
})