title: Angular: custom filter infinite digest
body: <p>I have a custom filter that takes in an Object, <strong>subjectBin</strong>, and returns a brand new Object, <strong>result</strong>.  The filter is invoked from a <strong>ng-repeat</strong>.  My custom filter works, but throws <strong>infinite $digest</strong> errors.  Here is my filter:</p>
  
  <pre><code>   return function(subjectBin, field) {
  
      var result = {},
          faculty,
          subject;
  
      angular.forEach(subjectBin, function (value, key) {
          faculty = key;
          angular.forEach(value, function (value, key) {
              subject = key;
              value.forEach(function (course) {
                  // Check "field" against some of the object's properties
                  //
                  if (course.asString.toUpperCase().indexOf(field) &gt; -1 ||
                      course.subjectTitle.toUpperCase().indexOf(field) &gt; -1 ||
                      faculty.toUpperCase().indexOf(field) &gt; -1 ) {
                      if (result.hasOwnProperty(faculty)) {
                          if (result[faculty].hasOwnProperty(subject)) {
                              result[faculty][subject].push(course);
                          }
                          else {
                              result[faculty][subject] = [course];
                          }
                      }
                      else {
                          result[faculty] = {};
                          result[faculty][subject] = {};
                          result[faculty][subject] = [course];
                      }
                  }
              });
          });
      });
  
      return result;
      };
  </code></pre>
  
  <p>To my understanding, this is giving <strong>infinite $digest</strong> errors because my filter is returning a <strong>brand new Object</strong> every time the $digest cycle happens.  And this causes the dirty-bit to be set again, and again and again...  </p>
  
  <p>But then I get confused when I see examples like this:</p>
  
  <pre><code>  .filter('reverse', function() {
  return function(input, uppercase) {
    input = input || '';
    var out = "";
    for (var i = 0; i &lt; input.length; i++) {
      out = input.charAt(i) + out;
    }
    // conditional based on optional argument
    if (uppercase) {
      out = out.toUpperCase();
    }
    return out;
  };
  })
  </code></pre>
  
  <p>This is an example from the Angular doc's, and it clearly takes in <strong>input</strong> and returns a brand new string <strong>out</strong>.  It also does <strong>not</strong> throw any <strong>infinite $digest</strong> errors.  In my eyes, this is analogous to my filter in that it returns a brand new Object. </p>
  
  <p>Any insight on why my filter is throwing <strong>infinite $digest</strong> errors, but then this other filter does not?</p>