title: angular: Validate multiple dependent fields
body: <p>Let's say I have the following (very simple) data structure:</p>
  
  <pre><code>$scope.accounts = [{
     percent: 30,
     name: "Checking"},
   { percent: 70,
     name: "Savings"}];
  </code></pre>
  
  <p>Then I have the following structure as part of a form:</p>
  
  <pre><code>&lt;div ng-repeat="account in accounts"&gt;
      &lt;input type="number" max="100" min="0" ng-model="account.percent" /&gt;
      &lt;input type="text" ng-model="account.name" /&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Now, I want to validate that the percents sum to 100 for each set of accounts, but most of the examples I have seen of custom directives only deal with validating an individual value. What is an idiomatic way to create a directive that would validate multiple dependent fields at once? There are a fair amount of solutions for this in jquery, but I haven't been able to find a good source for Angular.</p>
  
  <p>EDIT: I came up with the following custom directive ("share" is a synonym for the original code's "percent"). 
  The share-validate directive takes a map of the form <code>"{group: accounts, id: $index}"</code> as its value.</p>
  
  <pre><code>app.directive('shareValidate', function() {
  return {
      restrict: 'A',
      require: 'ngModel',
      link: function(scope, elem, attr, ctrl) {
          ctrl.$parsers.unshift(function(viewValue) {
              params = angular.copy(scope.$eval(attr.shareValidate));
              params.group.splice(params.id, 1);
              var sum = +viewValue;
              angular.forEach(params.group, function(entity, index) {
                  sum += +(entity.share);
              });
              ctrl.$setValidity('share', sum === 100);
              return viewValue;
          });
      }
  };
  });
  </code></pre>
  
  <p>This ALMOST works, but can't handle the case in which a field is invalidated, but a subsequent change in another field makes it valid again. For example:</p>
  
  <pre><code>Field 1: 61
  Field 2: 52
  </code></pre>
  
  <p>If I take Field 2 down to 39, Field 2 will now be valid, but Field 1 is still invalid. Ideas?</p>