Reputation: 3418
So I am trying to make an infinite scroll in Angular. The data items I want to show on my page are being served by a third party API and the URL looks like:
http://example.com/item/data/?param=item&pagesize=100&page=1
Now I am making a success ajax call using $http to the server and I have created a directive that on scroll will load the more Items from the url. But I can't get to update the URL, as soon as I start scrolling I would want my URL to become like:
http://example.com/item/data/?param=item&pagesize=100&page=1
http://example.com/item/data/?param=item&pagesize=100&page=2
http://example.com/item/data/?param=item&pagesize=100&page=3
http://example.com/item/data/?param=item&pagesize=100&page=4
....
However everytime I scroll the URL just becomes:
http://example.com/item/data/?param=item&pagesize=100&page=1
http://example.com/item/data/?param=item&pagesize=100&page=1
http://example.com/item/data/?param=item&pagesize=100&page=1
http://example.com/item/data/?param=item&pagesize=100&page=1
which means the page parameter is never updated or even if it is the Shttp never executes it properly. Can some one tell me how to update the URL param in $http which pulls data from thirs party API.
The code looks roughly something like below:
var page = 1;
var url = 'http://example.com/item/data/?param=item&pagesize=100&page=' + page;
$http.get(url)
.success(function(data, status, config, headers) {
$scope.page = 1;
for (var i = 0; i < data.items.length; i++) {
$scope.totalTags = data.items;
$scope.page += 1;
}
$scope.loading = false;
})
.error...//rest of the stuff
Upvotes: 0
Views: 106
Reputation: 1272
Several issues with your code :
Try this code snippet instead :
$scope.page = 1;
var url = 'http://example.com/item/data/?param=item&pagesize=100&page=' + $scope.page;
$http.get(url)
.success(function(data, status, config, headers) {
for (var i = 0; i < data.items.length; i++) {
$scope.totalTags.push(data.items[i]);
}
$scope.page += 1;
$scope.loading = false;
})
.error...//rest of the stuff
Upvotes: 1
Reputation: 13233
Seperate the url and parameters how the function allows you to, then it makes it much easier to make changes to your parameters.
var url="http://example.com/item/data/";
var params = {
param:"item",
pagesize:199,
page:page
};
params.page = 2;
$.get(url,params,function(data){});
Upvotes: 0