I'm making a pagination list image using AngularJS
. ng-click
doesn't work when I use it in <li>
tag or even on <a>
tag in it. But it's working fine in <button>
tag.
<div ng-controller="sampleCtrl" ng-app="myApp" data-ng-init="currentpage=1">
<h2>{{currentpage}}</h2>
<ul>
<li data-ng-repeat="image in images[currentpage-1]">
<img src='{{ image.img }}' width="150px" height="88px"/>
</li>
</ul>
<ul class="pagination">
<li data-ng-repeat="i in [1,2,3,4,5]" title="Page {{i}}">
<a href="#" data-ng-click="currentpage = currentpage + 1">{{i}}</a>
</li>
</ul>
<button data-ng-click="currentpage = currentpage + 1">Click me!</button>
</div>
Anyone knows what's the problem?
BTW, I tried this answer, but it's not working.
It's because ng-repeat
creates a new scope for each repeated element. And when you're doing currentpage = currentpage + 1
, you're most probably creating a new variable inside a child scope.
Check your scopes. The solutions is usually something as simple as $parent.currentpage = $parent.currentpage + 1
(to address the currentpage
variable in the parent scope, the sampleCtrl
scope) if you don't want to rewrite the whole thing.
Another solution would be creating a function to increase the page number, that way you won't be creating any new child scope variables, but would inherit the increase function from the controller:
<a href="#" data-ng-click="increasePage()">{{i}}</a>
And in your controller something like this:
$scope.increasePage = function() {
currentpage++;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With