[solved] How to reload/refresh with infinite-scroll


#1

i started on friday for the first time with the ionic framework to create my own small newsreader app for my blog. Im also a complete beginner on angular.

So here is my problem, I want to have a reload function.

My app should clear all data by using the reload function and grab the feed again. Event that works, but it shows me only the first 5 news (limit of my api/per page) and ignores completly the loadMore function.

factory

.factory('newsDataService', function($http) {
return {
  GetPosts: function(page) {
    return $http.get("http://newsapi.domain.tdl/");
  },
  GetMorePosts: function(page) {
    return $http.get("http://newsapi.domain.tdl/?page=" + page);
  }
  };
  })

controller

.controller('NewsCtrl', function($scope, newsDataService) {
$scope.page = 1;
$scope.noMoreItemsAvailable = false;

newsDataService.GetPosts().then(function(items){
  $scope.items = [];
  $scope.items = items.data.response;
});

$scope.doRefresh = function() {
  console.log('reload');
    newsDataService.GetPosts().then(function(items){
      console.log(items);
         $scope.items = items.data.response ;

         $scope.noMoreItemsAvailable = false;
         $scope.$broadcast('scroll.refreshComplete');
      })
  };

  $scope.loadMore = function(argument) {
    $scope.page++;
    newsDataService.GetMorePosts($scope.page).then(function(items){
          if (items.data.response) {
          $scope.items = $scope.items.concat(items.data.response);
            $scope.noMoreItemsAvailable = false;
          } else {
            $scope.noMoreItemsAvailable = true;
          }
      }).finally(function() {
        $scope.$broadcast("scroll.infiniteScrollComplete");
        });
  };
})

template

<ion-view view-title="News">
<ion-content>
<ion-refresher on-refresh="doRefresh()"></ion-refresher>
<div class="list">
<a collection-repeat="news in items" href="#/app/newsreader/{{news.id}}" class="item item-thumbnail-left"> 
  <h2>{{news.headline}}</h2>
  <div class="item-text-wrap" ng-bind-html="news.teaser"></div>
  </a>
  </div>
  <ion-infinite-scroll ng-if="!noMoreItemsAvailable" on-infinite="loadMore()" distance="1%"></ion-infinite-scroll>
  </ion-content>
  </ion-view>

How can I resolve this?


#2

Just found the solution on my own.

.controller('NewsCtrl', function($scope, newsDataService) { 
$scope.items =[]
$scope.page = 1;

newsDataService.GetPosts().then(function(items){
  $scope.items = items.data.response;
});

$scope.Reload = function() {
        console.log('reload');
        $scope.items =[];
        $scope.page = 1;
        $scope.loadMore();
  };

  $scope.loadMore = function(argument) {

    $scope.page++;
    newsDataService.GetMorePosts($scope.page).then(function(items){
          if (items.data.response) {
          $scope.items = $scope.items.concat(items.data.response);
            $scope.noMoreItemsAvailable = false;
          } else {
            $scope.noMoreItemsAvailable = true;
          }
      }).finally(function() {
        $scope.$broadcast("scroll.infiniteScrollComplete");
        $scope.$broadcast('scroll.refreshComplete');
        });
  };
})