0

I have a function that returns an object tree structure. But this tree structure contains a circular reference. (Note: I need this circularity in my data model, So I can't change it). I attached this object to the scope like this:

$scope.tree = myService.getTree(); //This return the circular structure

Then I tried to display this in my html file like this:

{{tree}}

But when I try to do this, it gives me this error:

TypeError: Converting circular structure to JSON

I know when I call {{tree}} in my html file, angularjs is calling JSON.stringify(tree) for me. My question is, how do I overwrite the behaviour on JSON.stringify in angular. For example can I tell angular or JSON.stringify to stop serialising the object tree if it detects a circularity in the object tree and return the json string it generated so far (instead of trowing an error).

Or alternatively, can I overwrite the behaviour to not call JSON.stringify for the whole object and instead call JSON.stringify or JSON.toString for individual objects inside the object tree as I access them. For example, if I call:

{{tree.person.name}}

then angular will only call stringify on the tree object and person object.

Wyetro
  • 8,439
  • 9
  • 46
  • 64
Rahal
  • 109
  • 2
  • 3
  • 6
  • Should read the following: http://stackoverflow.com/questions/10392293/stringify-convert-to-json-a-javascript-object-with-circular-reference – tymeJV Jul 06 '16 at 17:53

1 Answers1

0

Try

$scope.tree = JSON.parse(myService.getTree())
NehaJ
  • 127
  • 6