How to load more elements with AngularJS?

泄露秘密 提交于 2019-12-03 03:47:25
Pankaj Parkar

You don't need to think of jQuery, as you could solve this problem easily by using AngularJS itself.

You could maintain a variable inside your controller, name it as limit, then increment the limit variable inside loadMore() function.

Markup

<div ng-repeat="elem in travel.cruise | limitTo:travel.limit" class="cruises">

  ....COntent here...

</div>

Controller

app.controller('TravelController', function($scope) {
    var vm = this;
    vm.cruise = cruises;
    vm.limit = 3;

    $scope.loadMore = function() {
      var increamented = vm.limit + 3;
      vm.limit = incremented > vm.cruise.length ? vm.cruise.length : increamented;
    };
});

Demo Plunkr

You can combine @Pankaj Parkar response with infiniteScroll so you dont need even the button.

No need of cracking any deep coding. Its simple to add just one line of code

<div ng-repeat="elem in travel.cruise | limitTo:travel.limit" class="cruises">

..NG Repeat..

</div>

Controller

$scope.loadMore = function() {
      vm.limit = vm.limit + 3;
};

Assigning variable to increase by the 3 or more number will make your code work easily.

Simple and easy trick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!