6

Let's say my API returns the following people array:

var p1 = new Person("Alice");
var p2 = new Person("Bob");
p1.Sibling = p2;
p2.Sibling = p1;
var people = new[] { p1, p2 };

To avoid circular references, it outputs the following JSON:

[  
   {  
      "$id":"1",
      "Name":"Alice",
      "Sibling":{  
         "$id":"2",
         "Name":"Bob",
         "Sibling":{  
            "$ref":"1"
         }
      }
   },
   {  
      "$ref":"2"
   }
]

However, Angular doesn't handle it. Instead, I end up with an array where the second object is just a literal object with a $ref property.

Googling around, I see Angular just doesn't support this, but I imagine there must be some way to get it working.

Pharylon
  • 9,796
  • 3
  • 35
  • 59
  • 3
    Sounds like your best bet is to run the result through an operation to resolve the references [this question](http://stackoverflow.com/questions/15312529/resolve-circular-references-from-json-object) has some relatively concise code snippets for doing that. The second answer has a few improvements compared to the first. – JLRishe Oct 23 '15 at 18:59
  • @charlietfl I assume $http could resolve any references before returning it in the promise. This is what many (non-javascript) JSON parsers do. – Pharylon Oct 23 '15 at 19:50
  • @JLRishe Thanks for the link. – Pharylon Oct 23 '15 at 19:51
  • alternative might be to copy objects at server to avoid circular references – charlietfl Oct 23 '15 at 19:52
  • @Pharylon, Is this issue of AngularJS or Chrome? We are trying the $ref method to reduce payload. Did you find any official links from Angular that they don't support the $ref? Could you please share any links? – Joy George Kunjikkuru Jun 14 '20 at 15:48

0 Answers0