1

I have a select tag with a function call in ng-options:

<select ng-model='selectedList'
        ng-options='list.id as list.label
                    for list in listService.computeLists(resume)'
>

that's causing a repeating infinite digest error.

angular.js:14525 Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!

Is there any way to have this function call in ng-options without getting that error? I've tried memoizing but I couldn't get it to work.

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Peter Weeks
  • 291
  • 2
  • 16
  • The solution is to return the same array object if the elements have not changed. See [AngularJS Error Reference - $rootScope:infdig Infinite $digest Loop](https://docs.angularjs.org/error/$rootScope/infdig). – georgeawg Apr 10 '19 at 16:32
  • Thanks! This worked – Peter Weeks Apr 10 '19 at 18:11

1 Answers1

1

This error occurs when the application's model becomes unstable and each $digest cycle triggers a state change and subsequent $digest cycle. AngularJS detects this situation and prevents an infinite loop from causing the browser to become unresponsive.

One common mistake is binding to a function which generates a new array every time it is called. For example:

<div ng-repeat="user in getUsers()">{{ user.name }}</div>
$scope.getUsers = function() {
  return [ { name: 'Hank' }, { name: 'Francisco' } ];
};

The solution is to return the same array object if the elements have not changed.

var users = [ { name: 'Hank' }, { name: 'Francisco' } ];

$scope.getUsers = function() {
  return users;
};

For more information, see

georgeawg
  • 48,608
  • 13
  • 72
  • 95
  • Alternatively, if you have data that might change, you can cache the results of the function and return the cached results unless the results have changed. `var cache = {}; $scope.getUsers = function(input) { if (cache[input]) return cache[input]; else return cache[input] = users; }' Granted, the 'getUsers' function isn't the best example for this because the users likely won't change based on the input, but I'm sure you get the idea. If you have a template that you reuse in multiple places and you need data specific to each instance, this works. – Peter Weeks Apr 11 '19 at 17:24