0

as html page

 <div class="posts" ng-repeat="s in news" ng-init="moreNews()">
      {{s.news}}
 </div>
 <input type="button" value="get data" onclick="moreNews()"/>

at controller

$scope.moreNews = function () {
       $http.get("test?p=" + $scope.pno)
                .then(function (response) {
                    console.log(response);
                    $scope.news = response.data.news;
                    $scope.pno++;
                  }, function (data) {
                    alert("Oops! Unable to get data from server")
                });
    };

Here I am getting data from server and showing to html page. Here first response is showin data properly. but for second response it is overriding the data I want to add next data to below the first one and so one.

How to append lists below the existing data?

3 Answers 3

3
$scope.moreNews = function () {
       $http.get("test?p=" + $scope.pno)
                .then(function (response) {
                    console.log(response);
                    for(var i=0;i<response.data.news.length;i++){
                      $scope.news.push(response.data.news[i]);
                    }                    
                    $scope.pno++;
                  }, function (data) {
                    alert("Oops! Unable to get data from server")
                });
    };
Sign up to request clarification or add additional context in comments.

2 Comments

Declare $scope.news = []; on page load
response.data.news.forEach(x=> $scope.news.push(x)); make it even shorter instead of for loop, cheers
2

Try out this code

$scope.moreNews = function () {
       $http.get("test?p=" + $scope.pno)
                .then(function (response) {
                    console.log(response);
                    $scope.news=$scope.news.concat(response.data.news);
                    $scope.pno++;
                  }, function (data) {
                    alert("Oops! Unable to get data from server")
                });
    };

Comments

1
$scope.news = [];

$scope.moreNews = function () {
   $http.get("test?p=" + $scope.pno)
            .then(function (response) {
                $scope.news.push(response.data.news);
                $scope.pno++;
              }, function (data) {
                alert("Oops! Unable to get data from server")
            });
};

The push() method - Adds one or more elements to the end of the same array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.