2015-05-12 36 views
0

我從服務器獲得前十個對象並將其存儲在$ scope.data中。它看起來像這樣:如何將新結果添加到現有變量?

$scope.check = function(search, count) {      
    request.getData(search, count).success(function(response) { 
     $scope.data = response; 
    }); 
}; 

當用戶向下滾動,我想未來十年對象HTML代碼添加到$ scope.data並重復這些20個對象與NG-重複。後來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-重複顯示它是這樣的: enter image description here

Resulst不分開。請幫幫我。也許,我必須將元素推送到$ scope.data,但是我必須在ng-repeat中更改哪些內容?我不知道。

+1

like'$ scope.data = $ scope.data.concat(response);' – Grundy

回答

0

您可以使用本機陣列功能CONCAT:

var recievedData = []; //at the begining 

$http.get('/url').success(function (responseData) { 
    $scope.data = recievedData.concat(responseData); 
}); 

但要儘量避免出現這種情況時,從服務器的數據已經在本地數據變量。那麼你需要使用underscorelodash來保持它的乾淨和獨特。

+1

[array.concat](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/ Array/concat):_ concat()方法返回一個** new ** array_,所以如果你簡單地做concat _ $ scope.data_沒有改變 – Grundy

+0

是的,對不起,'$ scope.data = $ scope.data.concat responseData);' –

+0

只是編輯你的答案:-) – Grundy

相關問題