title: AngularJS: ng-repeat with dynamic list, without rebuilding entire DOM tree?
body: <p>I'm using ng-repeat on a table row with data from a JSON array retrieved from a server.  My goal is to have the list update automatically whenever an item is added, removed, or modified on the server, without affecting the unmodified items.  In the final implementation, these table rows will also contain bidirectionally bound <code>&lt;input&gt;</code> and <code>&lt;select&gt;</code> elements to send updates back to the server.  Some of the available options in the <code>&lt;select&gt;</code> elements will also be generated using ng-repeat directives from another list that may also change.</p>
  
  <p>So far, every time a new array comes from the server (currently polled every two seconds), the entire ng-repeat list is deleted and regenerated.  This is problematic because it interferes with text selection, destroys input fields even if they are currently being edited by the user, and probably runs a lot more slowly than necessary.</p>
  
  <p>I've written other web apps that do what I want using jQuery and DOM manipulation, but the code ends up being really hairy and development is time consuming.  I'm  hoping to use AngularJS and data binding to accomplish this in a fraction of the code and time.</p>
  
  <p><strong>So here's the question:</strong> is it possible to update the backing array this way, but only modify the DOM elements corresponding to the items/properties that actually changed?</p>
  
  <hr>
  
  <p>Here's a minimal test case that simulates periodic polling using a hard-coded array in a timer (see it live at <a href="http://jsfiddle.net/DWrmP/" rel="noreferrer">http://jsfiddle.net/DWrmP/</a>).  Notice that the text selection is cleared every 500ms due to the elements being deleted and recreated.</p>
  
  <h3>HTML</h3>
  
  <pre class="lang-html prettyprint-override"><code>&lt;body ng-app="myApp"&gt;
      &lt;table ng-controller="MyController"&gt;
          &lt;tr ng-repeat="item in items | orderBy:'id'"&gt;
              &lt;td&gt;{{item.id}}&lt;/td&gt;
              &lt;td&gt;{{item.data}}&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/table&gt;
  &lt;/body&gt;
  </code></pre>
  
  <h3>JavaScript</h3>
  
  <pre class="lang-js prettyprint-override"><code>angular.module('myApp', []).controller(
      'MyController', [
          '$scope', '$timeout',
          function($scope, $timeout) {
              $scope.items = [
                  { id: 0, data: 'Zero' }
              ];
  
              function setData() {
                  $scope.items = [
                      { id: 1, data: 'One' },
                      { id: 2, data: 'Two' },
                      { id: 5, data: 'Five' },
                      { id: 4, data: 'Four' },
                      { id: 3, data: 'Three' }
                      ];
                  $timeout(setData, 500);
              }
              $timeout(setData, 500);
          }
          ]
  );
  </code></pre>