I get first ten objects from server and store it in $scope.data. It looks like this:
我从服务器获得前十个对象并将其存储在$ scope.data中。它看起来像这样:
$scope.check = function(search, count) {
request.getData(search, count).success(function(response) {
$scope.data = response;
});
};
When user scrolls down I'd like to add next ten objects to $scope.data and repeat these 20 objects in html with ng-repeat. Later 30, 40, etc..
当用户向下滚动时,我想将下十个对象添加到$ scope.data中,并使用ng-repeat在html中重复这20个对象。后来30,40等。
<ul scroll>
<li ng-repeat="element in data">
{{element.id}}
{{element.name}}
{{element.surname}}
</li>
</ul>
This is my php:
这是我的PHP:
while ($data = mysqli_fetch_array($result)) {
$array[] = array("id"=>(int) $data['ID'], "name"=>$data['FirstName'], "surname"=>$data['LastName'], "mobile"=>$data['MobilePhone']);
}
echo json_encode($array);
And it returns something like this:
它返回如下内容:
[{"id":885,"name":"Jeremy","surname":"Goodrow","mobile":"+48-792-518-774"},{"id":886,"name":"Noella","surname":"Modestino","mobile":"+48-792-518-774"},{"id":887,"name":"Lief","surname":"Regoli","mobile":"+48-792-518-774"}]
So, my question is: how can I add new ten results to existing variable ($scope.data)? When I add it at first time I do this and it works great:
所以,我的问题是:如何将新的十个结果添加到现有变量($ scope.data)?当我第一次添加它时,我这样做并且效果很好:
$scope.data = response;
But how must I add new ten results to this variable? Response from the server is the same each time. I can't do something like this, because it does'nt work:
但是我该如何向这个变量添加新的十个结果呢?每次来自服务器的响应都是相同的。我不能做这样的事情,因为它不起作用:
$scope.data += response;
Also I can't push response to $scope.data, because ng-repeat shows it like this:
另外我无法将响应推送到$ scope.data,因为ng-repeat显示如下:
Resulst are not separated. Please, help me. Maybe, I must push elements to $scope.data, but what must I change in my ng-repeat? I don't know.
结果不分开。请帮我。也许,我必须将元素推送到$ scope.data,但是我必须在ng-repeat中改变什么?我不知道。
1 个解决方案
#1
You can use native array function concat:
您可以使用本机数组函数concat:
var recievedData = []; //at the begining
$http.get('/url').success(function (responseData) {
$scope.data = recievedData.concat(responseData);
});
But try to avoid this situation when the data from the server is already in the local data variable. Then you need to use underscore or lodash to keep it clean and unique.
但是,当来自服务器的数据已经在本地数据变量中时,请尽量避免这种情况。然后你需要使用下划线或lodash来保持它的清洁和独特。
#1
You can use native array function concat:
您可以使用本机数组函数concat:
var recievedData = []; //at the begining
$http.get('/url').success(function (responseData) {
$scope.data = recievedData.concat(responseData);
});
But try to avoid this situation when the data from the server is already in the local data variable. Then you need to use underscore or lodash to keep it clean and unique.
但是,当来自服务器的数据已经在本地数据变量中时,请尽量避免这种情况。然后你需要使用下划线或lodash来保持它的清洁和独特。