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.
P.S. 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 🙂
Try
instead
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…Although I am getting an extra blank item at the bottom of 10 items.