I have a custom filter that takes in an Object, subjectBin, and returns a brand new Object, result. The filter is invoked from a ng-repeat. My custom filter works, but throws infinite $digest errors. Here is my filter:
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) > -1 ||
course.subjectTitle.toUpperCase().indexOf(field) > -1 ||
faculty.toUpperCase().indexOf(field) > -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;
};
To my understanding, this is giving infinite $digest errors because my filter is returning a brand new Object every time the $digest cycle happens. And this causes the dirty-bit to be set again, and again and again...
But then I get confused when I see examples like this:
.filter('reverse', function() {
return function(input, uppercase) {
input = input || '';
var out = "";
for (var i = 0; i < input.length; i++) {
out = input.charAt(i) + out;
}
// conditional based on optional argument
if (uppercase) {
out = out.toUpperCase();
}
return out;
};
})
This is an example from the Angular doc's, and it clearly takes in input and returns a brand new string out. It also does not throw any infinite $digest errors. In my eyes, this is analogous to my filter in that it returns a brand new Object.
Any insight on why my filter is throwing infinite $digest errors, but then this other filter does not?
return memoize(function() { // your filter });, though I prefer my stabilize service:return stabilize(function() { // your filter });