title: Where should I put AngularJS scope $watchers?
body: <p>I'm trying to get changes to an AngularJS scope to trigger actions in a model. To do this I use <code>$scope.$watch()</code> in my item controller, and plug those controllers into directives. The problem I'm having is that when an item appears more than once on a page I'm getting multiple watchers for the same piece of data. </p>
  
  <p>For example:</p>
  
  <pre><code>angular.module("MyApp", [])
  .factory('ItemsModel', function() {
      var item1 = { triggers: 0, catches: 0 };
      var item2 = { triggers: 0, catches: 0 };
  
      return {
          getItems: function() { 
              return [item1, item2, item1];
          }
      };
  })
  .controller('AppCtrl', function($scope, ItemsModel) {
      $scope.items = ItemsModel.getItems();    
  })
  .controller('ItemCtrl', function($scope) {
      $scope.trigger = function() {
          $scope.item.triggers++;
      };    
      $scope.$watch('item.triggers', function(newValue, oldValue) {
          if(newValue != oldValue)
              $scope.item.catches++;
      });
  })
  .directive('item', function() {
      return {
          scope: {
              item: "="
          },
          controller: 'ItemCtrl'
      };
  });
  </code></pre>
  
  <p>Here <code>item1</code> appears twice in the <code>items</code> array, so I end up with two controllers watching the same piece of data. If <code>triggers</code> were changes to a property, and <code>catches</code> were saves back to a server, then I'd be saving each change to item1 twice. If an item appeared three times, it would save three times. </p>
  
  <p>I feel like the change listener should go in the model, but models don't have any access to the scope. Iterating over each item and adding behavior in the AppCtrl doesn't make a lot of sense to me. So where to the watchers go? </p>
  
  <p>I've also put this in a fiddle here: <a href="http://jsfiddle.net/nicholasstephan/p56MT/3/" rel="nofollow">http://jsfiddle.net/nicholasstephan/p56MT/3/</a></p>
  
  <p>Thanks</p>