简体   繁体   中英

Ionic Framework infinite scroll with http get data

I've just started using ionic framework (brilliant) and i'm new to angular. I have a web service running and the app is supposed to load data. I've managed to retrieve data, put, WordPress rest API is sending data in form of pages, and i don't know how to achieve that with infinite-scroll on tabs, here is my HTML markup:

<ion-view title="All Wallpapers">
  <ion-content ng-controller="MyCtrl">
    <div class="row" >
    <div ng-controller="PostsController" >
        <div ng-repeat="post in posts">
            <div class="col col-50">
                    <div class="list card">
                          <div class="item item-image">
                            <img ng-src="{{post.thumbnail}}"></>
                          </div>
                          <a class="item item-icon-left assertive" href="#">
                            <i class="icon ion-android-image"></i>
                            {{post.thumbnail_images.full.width}} x {{post.thumbnail_images.full.height}}
                          </a>
                    </div>
            </div>
    </div>
  </div>
  </div>
     <ion-infinite-scroll ng-if="!noMoreItemsAvailable" on-infinite="loadMore()" distance="1%"></ion-infinite-scroll>
 </ion-content>
</ion-view>

And in my apps.js i have:

wallweight.controller('PostsController', function($scope, $http) {
    $scope.page = 1;

    var url='http://wallweight.com/api/get_recent_posts?page';
    $http.get(url).
    success(function(data, status, headers, config) {

        $scope.posts = data.posts;
        console.log(data);
    }
).


    error(function(data, status, headers, config) {

    });


});
function MyCtrl($scope, $http) {
  $scope.posts = [];
  $scope.loadMore = function() {
    $http.get('http://wallweight.com/api/get_recent_posts?page=2').success(function(items) {
      $scope.posts.push($scope.posts.items);
      $scope.$broadcast('scroll.infiniteScrollComplete');
    });
  };

  $scope.$on('$stateChangeSuccess', function() {
    $scope.loadMore();
  });
}

The second function does load the data, but does not append it to the original data. Thank you for your time.

PS I've viewed many codepen examples, however, i cant seem to get them working.

EDIT:

Okay, I've managed to narrow down the problem. I was misusing the controllers now i get the data, but the new data replaces the old data.

  function MyController($scope, $http) {
  $scope.posts = [];
  $scope.page=1;
  $scope.loadMore = function() {
    $http.get('http://wallweight.com/api/get_recent_posts?page='+$scope.page).success(function(items) {
      $scope.posts=items.posts;
      console.log(items.posts);
      $scope.$broadcast('scroll.infiniteScrollComplete');
      console.log($scope.page);
      $scope.page +=1;
    });
  };

I understand $scope.posts = items.posts is wrong, i just cant find a way to append it.

Got it working :)

function MyController($scope, $http) {
  $scope.posts = [];
  $scope.page=1;
  $scope.loadMore = function() {
    $http.get('http://wallweight.com/api/get_recent_posts?page='+$scope.page).success(function(items) {
     var i = items.posts.length;
     $scope.posts = $scope.posts.concat(items.posts);
     $scope.posts.push(items);
      console.log(items.posts[0]);
      $scope.$broadcast('scroll.infiniteScrollComplete');
      console.log($scope.page);
      $scope.page +=1;
    });
  };

Try

$scope.posts = $scope.posts.concat(items.posts);

instead

$scope.posts=items.posts;

comment this line //$scope.newsletters.push(data); it add as another item entire data.

Thank you this saved me hours.

I modified mine to work with $resource for my Factory but it's essentially the same...

    $scope.loadMore = function() {
    moreNewsletters = WPFactory.query({
        'page' : $scope.pageNum,
    'filter[posts_per_page]': 10,
    param1: 'posts',
    'filter[cat]': 8,
    'filter[orderby]': 'ID',
    }, function(data,responseHeaders) {

        angular.forEach(moreNewsletters, function(value, key) {
        value.image1 = ThumbnailFactory.getThumbnail(value.content);
    });

 var i = data.length;
 $scope.newsletters = $scope.newsletters.concat(data);
 $scope.newsletters.push(data);
  $scope.$broadcast('scroll.infiniteScrollComplete');
  $scope.pageNum +=1;
    });    
};

Although I am getting an extra blank item at the bottom of 10 items.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM