我刚刚开始使用离子框架(辉煌)而且我刚开始使用角度。我有一个Web服务正在运行,应用程序应该加载数据。我设法检索数据,put,wordpress rest api正在以页面的形式发送数据,我不知道如何通过无限滚动选项卡实现这一点,这是我的html标记:
<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>
在我的apps.js中我有:
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();
});
}
第二个函数会加载数据,但不会将其附加到原始数据。 谢谢你的时间。
P.S,我已经看到了许多代码集示例,但是,我似乎无法让它们正常工作..
编辑:
好吧,我设法缩小了问题范围。我误用了控制器 现在我得到了数据,但新数据取代了旧数据,
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;
});
};
我理解$ scope.posts = items.posts是错误的,我只是找不到附加方法
答案 0 :(得分:5)
搞定了工作:)
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;
});
};
答案 1 :(得分:2)
尝试
$scope.posts = $scope.posts.concat(items.posts);
代替
$scope.posts=items.posts;
答案 2 :(得分:0)
评论此行//$scope.newsletters.push(data); 它将整个数据添加为另一项。
答案 3 :(得分:-1)
谢谢你,这节省了我几个小时。
我修改了我的工作$resource
为我的工厂,但它基本上是相同的......
$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;
});
};
虽然我在10个项目的底部得到了一个额外的空白项目。