I'm trying to create a infinite scroll feature in my application but it feels a bit abstract. I want to use ui-scroll and this fiddle shows a simple example of how it works.
I've read the readme and looked through some examples also I've integrated the example in my project and got it working, but I can't figure out on how to combine this with data from my own database.
I have a database table called movies. The movies have a few values such as title
, release_date
, image_url
How would I insert that data into the $scope.movieDataSource
so I can use it in my view?
$http.get(('/movies.json'), {
cache: true
})
.success(function(data, status, headers, config) {
if (status == 200) {
$scope.userMovies = data;
} else {
console.error('Error happened while getting the user list.')
}
$scope.movieDataSource = {
get: function(index, count, callback) {
var i, items = [$scope.userMovies], item;
var min = 1;
var max = 1000;
for (i = index; i < index + count; i++) {
if (i < min || i > max) {
continue;
}
item = {
title: $scope.userMovies.title,
imageURL: $scope.userMovies.poster_path
};
items.push(item);
}
callback(items);
}
}
});
I've tried to create an example of what I'm trying to get at. I use a http.get to fill my userMovies
scope with records from my database and I want to use those records as items in the movieDataSource
.
But when I visit the page I that ui-scroll
does add results in the container, but it does not show content.
<div class="imageCell ng-scope" ui-scroll="item in movieDataSource">
<img title="">
</div>
If I console.log("movieDataSource" + $scope.movieDataSource)
it shows me movieDataSource[object Object]
.
You are making this more complex than necessary. The
uiScroll
directive is a replacement forngRepeat
, which takes a Data Source with 3 properties:in your case, you have an array of items. Each time the
index
orcount
changes, thesuccess
fires, and this function should return a subset of your array fromindex
toindex + count
. There are multiple ways to accomplish this. The example you posted uses a for loop to iteratively push items into the array. You could also use theArray.slice()
method.Option 1:
Option 2:
As for your HTML, it should be identical to if you were using ng-repeat: