Error thrown when $ watch object with circular references

When $ looks at an object with circular references eg. xy = y; yx = x;

Error: Recursion Too Much. See code below.

How do I customize the behavior of $ watch? or create a custom equals () function for the object?

<!DOCTYPE html>
<html>
<body>
    <div ng-app="" ng-controller="testController">
        {{complex .v}},{{complex.y.v}}
    </div>
</body>

<script>
    function testController($scope) {

        var x = { v: 5, y: null };
        var y = { v: 6, x: x };

        x.y = y;    // <---------------- circlular ref


        $scope.$watch('complex', function(newVal){

        }, true);

        $scope.complex = x;

    }
</script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>

</html>

      

+3


source to share


1 answer


You can specify a custom function in your watch. For example.



$scope.$watch(function() {
   //custom equality handler, return a value that when changed will fire the handler
}, function(newVal) {
   console.log('complex changed');
});

      

+1


source







All Articles