I guess this means there is a circular reference somehwere but for the life of me I can't guess how to fix it.
Anyone have any ideas?
http://plnkr.co/edit/aNcBcU?p=preview
Check the debug console in Chrome (for example) and you'll see the error. The offending line is
scope.map = map;
scope.map is being "$watched" on the controller via
$scope.$watch("options.map", function (map) { ... }, true);
The call stack is limited in size, and when it's exceeded, the RangeError is thrown. This can happen when a deeply nested function is called, or when a lot of new variables are created. The most common way to fix this error is to reduce the number of function calls, or to limit the number of variables that are created.
The JavaScript RangeError: Maximum call stack size exceeded is an error that occurs when there are too many function calls, or if a function is missing a base case.
It means that somewhere in your code, you are calling a function which in turn calls another function and so forth, until you hit the call stack limit. This is almost always because of a recursive function with a base case that isn't being met.
The "RangeError: Maximum call stack size exceeded" error occurs when a function is called so many times that the invocations exceed the call stack limit. To solve the error, specify a base case that has to be met to exit the recursion.
It's because you're comparing for object for equality rather than for reference. Change your $watch
statement to this:
$scope.$watch("options.map", function (map) {
if (map === undefined) {
alert("map has no value");
} else {
alert("map is defined");
}
});
I also had this issue and found out that the objects I was comparing had circular references. (Tried JSON.stringify()
which threw 'TypeError: Converting circular structure to JSON').
When I edited my object so that it didn't have circular structure, I fixed this problem and compared objects not by reference but by properties value which was what I needed.
The third parameter of $watch function tells how to compare the watched object. False to reference comparing only. True to recursive equality comparing, if an object contains circular references, then over maximum stack size. For example:
var a = {ref:b};
var b = {ref:a};
$scope.$watch('b', function(){
//code here will never called, because stack overflow when comparing the object b.
}, true)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With