[英]Ionic infinite scroll
I am using wordpress as a backend for an app and I want to use infinite scroll but I am having trouble concatenating articles. 我使用wordpress作为应用程序的后端,我想使用无限滚动但我无法连接文章。
I am calling the service using a factory: 我正在使用工厂调用该服务:
.factory('Worlds', function ($http) {
var worlds = [];
storageKey = "worlds";
function _getCache() {
var cache = localStorage.getItem(storageKey );
if (cache)
worlds = angular.fromJson(cache);
}
return {
all: function () {
return $http.get("http://www.examplesite.com/tna_wp/wp-json/posts?filter[category_name]=international&filter[posts_per_page]=10").then(function (response) {
worlds = response.data;
console.log(response.data);
return worlds;
});
},
GetNewPosts: function () {
return $http.get("http://www.examplesite.com/tna_wp/wp-json/posts?filter[category_name]=international&filter[posts_per_page]=2").then(function (response) {
worlds = response.data;
console.log(response.data);
return worlds;
});
},
get: function (worldId) {
if (!worlds.length)
_getCache();
for (var i = 0; i < worlds.length; i++) {
if (parseInt(worlds[i].ID) === parseInt(worldId)) {
return worlds[i];
}
}
return null;
}
}
})
and my controller looks like this: 我的控制器看起来像这样:
.controller('WorldCtrl', function ($scope, $stateParams, $timeout, _, Worlds) {
$scope.worlds = [];
Worlds.all().then(function (data){
$scope.worlds = data;
window.localStorage.setItem("worlds", JSON.stringify(data));
},
function (err) {
if(window.localStorage.getItem("worlds") !== undefined) {
$scope.worlds = JSON.parse(window.localStorage.getItem("worlds"));
}
}
);
$scope.loadMore = function() {
Worlds.GetNewPosts().then(function (worlds){
var loadedIdss = _.pluck($scope.worlds, 'id');
var newItemss = _.reject(worlds, function (item){
return _.contains(loadedIdss, item.id);
});
$scope.worlds = newItemss.concat($scope.worlds);
$scope.$broadcast('scroll.infiniteScrollComplete');
});
};
})
I am trying to use underscore to ignore the posts that are already loaded, however when i try the infinite scroll it just goes into a loop calling more posts but doesnt add them to my ng-repeat and ionicLoading renders the app useless. 我试图使用下划线来忽略已经加载的帖子,但是当我尝试无限滚动它只是进入一个循环调用更多的帖子,但没有添加到我的ng-repeat和ionicLoading呈现应用程序无用。
ion-infinite-scroll
works with some sort of paginated result and you seem to feed your list with all the results. ion-infinite-scroll
与某种分页结果一起工作,你似乎用你的所有结果提供你的列表。
Your API should look something like this: 您的API应如下所示:
http://www.examplesite.com/tna_wp/wp-json/posts?filter[category_name]=international&filter[posts_per_page]=2&filter[page]=1
notice I've added a page filter. 注意我添加了一个页面过滤器。
and your service responsible to fetch the data should look something like this: 负责获取数据的服务应该如下所示:
.factory('dataService', function($http) {
return {
GetPosts: function(page, pageSize) {
return $http.get("http://mywebservice/api/test/posts/" + page + "/" + pageSize);
}
};
});
Your controller 你的控制器
.controller('mainController', function($scope, dataService) {
$scope.posts = [];
$scope.theEnd = false;
var page = 0;
var pageSize = 10;
$scope.$on('$stateChangeSuccess', function() {
$scope.loadMore();
});
$scope.loadMore = function(argument) {
page++;
dataService.GetPosts(page, pageSize)
.then(function(result) {
console.log('items fetched: ' + result.data.length);
if (result.data.length > 0) {
angular.forEach(result.data, function(value, key) {
$scope.posts.push(value);
});
}
else {
$scope.theEnd = true;
}
})
.finally(function() {
$scope.$broadcast("scroll.infiniteScrollComplete");
});
};
})
would initialize an array of objetct - as you're doing - and a boolean which tells the directive ion-infinite-scroll
when you've reached the end: 会初始化一个objetct数组 - 就像你正在做的那样 - 以及一个布尔值,当你到达终点时它会告诉指令ion-infinite-scroll
:
$scope.posts = [];
$scope.theEnd = false;
Then you can have some variables to control the pagination: 然后你可以有一些变量来控制分页:
var page = 0;
var pageSize = 10;
I start loading when the view is loaded: 我在加载视图时开始加载:
$scope.$on('$stateChangeSuccess', function() {
$scope.loadMore();
});
$scope.loadMore
then would increment the page number: $scope.loadMore
然后会增加页码:
page++;
and call the service layer: 并调用服务层:
dataService.GetPosts(page, pageSize)
when I've reached the end of the stream I would set the variable: 当我到达流的末尾时,我会设置变量:
$scope.theEnd = true;
to let the directive know we don't have other items to append. 让指令知道我们没有其他要追加的项目。
.finally(function() {
$scope.$broadcast("scroll.infiniteScrollComplete");
});
finally
is always called when the promise is resolved. finally
,当承诺解决总是被调用。
Instead of ng-repeat
you can use collection-repeat which should be much faster. 而不是ng-repeat
你可以使用collection-repeat ,它应该更快。
Try this create a function infiniteScrollCompleteCancelLoadMore
and call it when you want to complete the scroll and you have reached the end of your list. 试试这个创建一个函数infiniteScrollCompleteCancelLoadMore
并在你想完成滚动时调用它,你已到达列表的末尾。
function infiniteScrollCompleteCancelLoadMore() {
$timeout(function () {
$timeout(function () {
$scope.$broadcast('scroll.infiniteScrollComplete');
$rootScope.canLoad = false;
});
});
}
$scope.loadMore = function() {
Worlds.GetNewPosts().then(function (worlds){
var loadedIdss = _.pluck($scope.worlds, 'id');
var newItemss = _.reject(worlds, function (item){
return _.contains(loadedIdss, item.id);
});
$scope.worlds = newItemss.concat($scope.worlds);
infiniteScrollCompleteCancelLoadMore() // CHANGE HERE
});
};
and your HTML 和你的HTML
<ion-infinite-scroll on-infinite="loadMore()" ng-if="canLoad" distance="1%"
immediate-check="false"></ion-infinite-scroll>
OR call This is you just want to cancel loadMore loop
. 或致电:这是你只是想取消loadMore loop
。
function infiniteScrollComplete() {
$timeout(function () {
$timeout(function () {
$scope.$broadcast('scroll.infiniteScrollComplete');
});
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.