title: AngularJS: Reassigning an object vs. replacing it in a controller
body: <p>I am building a subscriber/observer pattern for displaying data in realtime for my angular app.</p>
  
  <p>The observer is built with a factory injected into the angular controller and whose role is to fetch data and update it. The basic code structure can he found in this fiddle: <a href="http://jsfiddle.net/ctrager/67QR7/3/" rel="nofollow">http://jsfiddle.net/ctrager/67QR7/3/</a></p>
  
  <pre><code>var myApp = angular.module('myApp', [])
  .factory('MyFactory', [function () {
  
      var Collection = {};
      Collection.isLoaded = 0;
      Collection.data = [1, 2];
      Collection.username = "corey and eric";
      Collection.update = function () {
          Collection.data.push(new Date())
      }
      Collection.replace = function () {
          // If you do Collection.data = []
          // here you are doing the same thing
          // as the empty collection bug.  I can't
          // tell you EXACTLY why this confuses angular
          // but I'm 99% sure it's the same phenomenon
          Collection.data = [new Date()]
      }
      Collection.replace_fixed = function () {
          // This works
          Collection.data.length = 0
          Collection.data.push(new Date())
      }
      return Collection;
  
  }])
  
  
  function MyCtrl($scope, MyFactory) {
      $scope.name = 'Eric';
      $scope.items = MyFactory.data;
      $scope.replace = function(){
          console.log("replace")
          MyFactory.replace()
          //$scope.items = MyFactor.data;
      }
      $scope.replace_fixed = function(){
          console.log("replace_fixed")
          MyFactory.replace_fixed()
          //$scope.items = MyFactor.data;
      }
      $scope.update = function(){
          console.log("update")
          MyFactory.update()
      }
  }
  </code></pre>
  
  <p>The factory (MyFactory) contains a collection (Collection.data). Any push (/splice) to that collection is reflected in the scope, but if I replace the entire collection (Collection.replace()) the change is no longer reflected in $scope. Any idea why?</p>