title: Concat Objects in AngularJS
body: <p>I'm using AngularJS to develop a single page app. Currently I have things setup so every 5 seconds a call is made to the DB to update the client-model. I wanted to stagger things with the timestamp. So it doesn't pull everything from the DB, but just pulls everything that has been added if newer than last call made. (conserve bandwidth and such). 
  But it's not making sense to me</p>
  
  <p><em>Angular Controller</em></p>
  
  <pre><code>var timestamp = '';
  function NewsListCtrl($scope, $http, $timeout) { 
      $scope.news = [];
      $scope.newsRequest = function() {
  
          $http.get('/ci/index.php/news/get_news_ajax/' + timestamp).success(function(data) {
              timestamp = $.now();
  
              $scope.news = $scope.news.concat(data);
          });
      };
  
      $scope.newsRequest();
      setInterval(function(){
          $scope.$apply(function(){
              $scope.newsRequest();
          });
      }, 5000);
  }
  </code></pre>
  
  <p>The above code doesn't print anything on the page. The first request shows data in the object. But subsequent requests (with timestamps) show empty objects. So at least the first request should print out.</p>
  
  <p><em>Sample of data in initial request</em></p>
  
  <pre><code>{"new":[{"id":"181","title":"not gonna work","slug":"not-gonna-work","time":"1363374669","text":"asdfs","deletetime":null}]}
  </code></pre>
  
  <p>And when I remove</p>
  
  <pre><code>$scope.news = [];
  </code></pre>
  
  <p>I then get errors returned saying that it is undefined.</p>
  
  <p>Anyone able to shed some light on the correct way of accomplishing this? </p>
  
  <p><em><strong>Edit:</em></strong></p>
  
  <p>Based on answers, I have updated my code to:</p>
  
  <pre><code>var timestamp = '';
  function NewsListCtrl($scope, $http, $timeout) { 
      $scope.news = [];
      $scope.newsRequest = function(){
          $http.get('/ci/index.php/news/get_news_ajax/' + timestamp).success(function(data) {
              timestamp = $.now();
              $scope.news = $scope.news.concat(data.new);   
      });
  };
  $scope.newsRequest();
  setInterval(function(){
      $scope.$apply(function(){
             $scope.newsRequest();
      });
  }, 5000);
  </code></pre>
  
  <p>So now data remains displayed on the page. But it no longer actually populates new data from the model. Not sure if this is an issue in the logic. I'm using </p>
  
  <pre><code>timestamp = $.now(); 
  </code></pre>
  
  <p>to set the current time. It displays a 13 digit int. But in my DB, it displays a 10 digit int with php's time function.</p>
  
  <pre><code>time()
  </code></pre>
  
  <p>Do these conflict?</p>