How to solve a RangeError when the maximum call stack size exceeded

When trying to copy an object with AngularJS’ copy() it might be that Angular throws a RangeError. This is the case if your object has circular dependencies which is troublesome with creating an angular deep copy.

If the following snippet was your code …

angular.forEach($scope.words, function(word, index) {
  var deep_copy = angular.copy(word);
  /* do something with your copy */

… you can easily fix the issue by using jQlite/jQuery’s extend(). This works perfectly with both, shallow and deep copies. Shallow copies duplicate as little as possible so you’d be better with omitting extend‘s first parameter.

angular.forEach($scope.words, function(word, index) {
  var shallow_copy = $.extend({}, word);  
  var deep_copy = $.extend(true, {}, word);
  /* do something with your copies */

1 Comment

  1. Priyanka


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>