Blynn Blynn - 4 months ago 8
Javascript Question

angular scope item +1 not returning multiple clicks

I am adding 1 to a value of zero which returns 1 and bring up the next item in the set which works. However if I click on the ng-click event again I was hoping it adds +1 to the same number returning 2 and then getting the next item, but it doesn't take a look below.

App.controller('slideCtrl', function ($scope, $filter) {

$scope.item = 0;

$scope.prev = function() {
$scope.item = -1;
};

$scope.next = function() {
$scope.item = +1;
};

});


HTML

<a ng-hide="x.Slide ==1" ng-click="prev();">Left Arrow</a>
<span ng-show="x.Slide ==1"></span>
<span class="card-Section">{{x.Slide}}/{{count()}}</span>
<a ng-click="next();">Right Arrow</a>

Answer

It's a simply logic fault, if you assign it:

$scope.item = -1;

You're telling the item to be -1 not to decrement.

To make it work you have to do it:

$scope.prev = function() {
    $scope.item--;
}; 

$scope.next = function() {
    $scope.item++;
};

Tip: I don't know what count() is, but for sure you must not call the function like this:

<span class="card-Section">{{x.Slide}}/{{count()}}</span>

It will make several calls in count() function.

Comments