我正在摆弄 ionic,并且使用运球数据只是为了玩玩。我不知道如何 A. 我可以查询 dribble 以显示前 15 条之后的帖子,API 在这里 http://api.dribbble.com/shots/everyone 如果我想获得更多,我可以请求第 2 页就像 http://api.dribbble.com/shots/everyone?page=2
B.如何将项目传递到我的模板中而不与旧项目冲突?
/**
* Dashboard Controller. (must be authenticated)
*
*/
.controller('DashController', function($scope, $state, DribbbleData, $http) {
$scope.items = [];
$scope.initialize = function() {
DribbbleData.getData().then(function(res) {
console.log(res);
$scope.items = res.data.shots;
}, function(status) {
console.log('error')
})
}
$scope.loadMore = function() {
$http.jsonp('http://api.dribbble.com/shots/everyone?callback=JSON_CALLBACK').success(function(items) {
console.log(items);
var test = $scope.items;
console.log(test)
//$scope.items = items;
//useItems(items);
$scope.$broadcast('scroll.infiniteScrollComplete');
});
};
$scope.$on('$stateChangeSuccess', function() {
$scope.loadMore();
});
$scope.initialize();
})
景色
<ion-view view-title="Dashboard">
<ion-content class="padding">
<ion-list>
<div class="list card" data-ng-repeat="item in items">
<div class="item item-title">
<h2>{{item.title}}</h2>
</div>
<div class="item item-body">
<img class="full-image" ng-src="{{item.image_url}}">
<p>
<a href="#" class="subdued">{{item.likes_count}} Like</a>
<a href="#" class="subdued">{{item.comments_count}} Comments</a>
</p>
</div>
<div class="item tabs tabs-secondary tabs-icon-left">
<a class="tab-item" href="#">
<i class="icon ion-thumbsup"></i>
Like
</a>
<a class="tab-item" href="#">
<i class="icon ion-chatbox"></i>
Comment
</a>
<a class="tab-item" href="#">
<i class="icon ion-share"></i>
Share
</a>
</div>
</div>
</ion-list>
<ion-infinite-scroll on-infinite="loadMore()" distance="1%">
</ion-infinite-scroll>
</ion-content>
</ion-view>
如果您确定没有重复项,只需连接项目即可:
$scope.items = $scope.items.concat(items);
如果不存在,最好的办法是跟踪列表中当前的项目,并仅在其尚不存在时添加:
var itemIds = {};
function addItems(items) {
items.forEach(function (item) {
if (itemsIds[item.id]) {
return;
}
$scope.items.push(item);
itemIds[item.id] = true;
}
}
要处理分页,只需在成功回调中增加一个计数器即可。例如:
var nextPage = 2;
$scope.loadMore = function() {
$http.jsonp('http://api.dribbble.com/shots/everyone?callback=JSON_CALLBACK&page=' + nextPage).success(function(items) {
nextPage++;
addItems(items);
$scope.$broadcast('scroll.infiniteScrollComplete');
});
};
当 ajax 完成时,您需要将新项目添加到您的
$scope.items
数组中
$scope.loadMore = function() {
$http.jsonp('http://api.dribbble.com/shots/everyone?callback=JSON_CALLBACK').success(function(items) {
// Add all the new items
items.forEach(function(newItem) {
$scope.items.push(newItem);
});
$scope.$broadcast('scroll.infiniteScrollComplete');
});
};