0

I get the following error in Angularjs:

"Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!
Watchers fired in the last 5 iterations: [["fn: parentValueWatch; newVal: {\"x\":0,\"y\":0}; oldVal: {\"x\":0,\"y\":0}"],["fn: parentValueWatch; newVal: {\"x\":0,\"y\":0}; oldVal: {\"x\":0,\"y\":0}"],["fn: parentValueWatch; newVal: {\"x\":0,\"y\":0}; oldVal: {\"x\":0,\"y\":0}"],["fn: parentValueWatch; newVal: {\"x\":0,\"y\":0}; oldVal: {\"x\":0,\"y\":0}"],["fn: parentValueWatch; newVal: {\"x\":0,\"y\":0}; oldVal: {\"x\":0,\"y\":0}"]]

The newVal and oldVal logs are all the same. According to How to Troubleshoot Angular "10 $digest() iterations reached" Error, the error should be thrown when the value changes.

This is my app code:

$scope.getCommentLocation = function(comment) {
        console.log("TEST");
        if (comment == undefined) { // This is all that gets returned every time
            return {
                x: 0,
                y: 0
            };
        }
        $range = getRangeObjectForComment(comment);
        position = $range.offset();
        window_position_y = position.top - $('#main-text_ifr').contents().find('body').scrollTop() + frameposition.top + $range.height() / 2;
        window_position_x = position.left + frameposition.left + $range.width() / 2;
        return {
            x: window_position_x,
            y: window_position_y
        };
    };

This is the jade code (Jade is an html template language):

location="getCommentLocation(commentEditor.activeComment)"
Community
  • 1
  • 1
sinθ
  • 11,093
  • 25
  • 85
  • 121

1 Answers1

1

The following code will always create a new object and returns it.

          return {
                x: 0,
                y: 0
            };

Use a referrence variable kind of $scope.origin which will be only defined once outiside the $scope.getCommentLocation. $scope.origin ={x:0, y:0};//never recreate this any where just use the origin referrence

and use return $scope.origin

guru
  • 4,002
  • 1
  • 28
  • 33