【发布时间】:2015-05-12 14:58:33
【问题描述】:
我从服务器获取前十个对象并将其存储在 $scope.data 中。它看起来像这样:
$scope.check = function(search, count) {
request.getData(search, count).success(function(response) {
$scope.data = response;
});
};
当用户向下滚动时,我想将接下来的十个对象添加到 $scope.data 并使用 ng-repeat 在 html 中重复这 20 个对象。后来 30、40 等。
<ul scroll>
<li ng-repeat="element in data">
{{element.id}}
{{element.name}}
{{element.surname}}
</li>
</ul>
这是我的 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);
它返回如下内容:
[{"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"}]
所以,我的问题是:如何将新的十个结果添加到现有变量 ($scope.data)?当我第一次添加它时,我这样做并且效果很好:
$scope.data = response;
但是我必须如何向这个变量添加新的十个结果呢?每次来自服务器的响应都是相同的。我不能这样做,因为它不起作用:
$scope.data += response;
我也无法将响应推送到 $scope.data,因为 ng-repeat 显示如下:
结果没有分开。请帮我。也许,我必须将元素推送到 $scope.data,但是我必须在我的 ng-repeat 中更改什么?我不知道。
【问题讨论】:
-
点赞
$scope.data = $scope.data.concat(response);
标签: javascript ajax json angularjs