title: Binding mutually dependent but different data formats to inputs in Angular
body: <p>As an exercise, I'm creating inputs for color values using both RGB and hex.</p>
  
  <p>html:</p>
  
  <pre><code>&lt;form ng-controller="myCtrl"&gt;
      R:&lt;input ng-model="rChannel" type="number" min="0" max="255" required&gt;&lt;/input&gt;
      G:&lt;input ng-model="gChannel" type="number" min="0" max="255" required&gt;&lt;/input&gt;
      B:&lt;input ng-model="bChannel" type="number" min="0" max="255" required&gt;&lt;/input&gt;
  
      hex: #&lt;input ng-model="hexColor" type="text" required&gt;&lt;/input&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>js:</p>
  
  <pre><code>function myCtrl($scope) {
      $scope.$watch('[rChannel, gChannel, bChannel]',
                function() {
                  $scope.hexColor = rgbToHex($scope.rChannel, $scope.gChannel, $scope.bChannel)
                },
                true);
  
      $scope.$watch('hexColor',
                function() {
                  var rgbArr = hexToRgbArray($scope.hexColor);
                  $scope.rChannel =  rgbArr[0];
                  $scope.gChannel =  rgbArr[1];
                  $scope.bChannel =  rgbArr[2];
                });
  }
  </code></pre>
  
  <p><a href="http://jsfiddle.net/F545z/" rel="nofollow">http://jsfiddle.net/F545z/</a></p>
  
  <p>It works... with one big hiccup. The moment any one input value goes invalid (an empty string, or the hex is less than six characters), all of the inputs disappear! This has the effect of deleting values that the user has already input. E.g. when the user types a valid 6-character hex value, and then presses the delete key to correct the last character of the hex, the entire hex value disappears, and needs to be retyped completely. If you watch in the console, you can see that what's occurring. I think it's correct behavior for the rgb inputs to disappear when the hex is invalid, but it clearly hampers the user to erase the value he/she is in the process of typing.</p>
  
  <p>This is clearly happening because of the "double-bind"-- the rgb and hex values are watching their own model, but also each other. There's some serious infinite loop potential here, and it's probably only working at all because <a href="http://docs.angularjs.org/api/ng.%24rootScope.Scope" rel="nofollow">the angular docs say the loop only runs 10x</a> to prevent an infinite loop deadlock.</p>
  
  <p>I'm pretty sure I'm doing this wrong way to begin with. Should I try to write a separate directive for the hex input? If so how should I link them all up? Is $watch legit for this kind of use? A working fiddle is most helpful.</p>