Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Using JSON in HTML attribute throws error #5296

Closed
bbrewder opened this issue Dec 5, 2013 · 3 comments · Fixed by #5385
Closed

Using JSON in HTML attribute throws error #5296

bbrewder opened this issue Dec 5, 2013 · 3 comments · Fixed by #5385

Comments

@bbrewder
Copy link

bbrewder commented Dec 5, 2013

If you use JSON in an HTML attribute that is used in a '=' scope variable, it throws the error "[$rootScope:infdig] 10 $digest() iterations reached. Aborting!".

Here is a simple Plunker that shows the error.

http://plnkr.co/edit/B3NZSXbGsNyJec4PaFd4?p=preview

Everything seems to work fine except for this error (it seems to bind OK anyway). Just passing the attribute value as a string and calling scope.$eval within the link function seems to work, though I'm not sure if you would get 2-way data binding that way if the value was passed as an object instead of JSON.

The code should either be fixed to support inline JSON or a more explicit error should be thrown if JSON is being sent in from the attribute. If an error is thrown, it would be nice to include in the documentation on it what the work around is (scope.$eval?) and any limitations (2-way data binding broken?).

@ghost ghost assigned jeffbcross Dec 5, 2013
@lord2800
Copy link

lord2800 commented Dec 6, 2013

The problem here is that you're creating a new object every time the watcher looks at the value, which causes $digest to think the value was dirty, which causes another $digest loop. You should assign that object to a $scope property (thereby only creating it once) or use ng-init (also creating it only once).

@bbrewder
Copy link
Author

bbrewder commented Dec 6, 2013

Thanks for responding. If you take a look at the sample code you will see that I only define the JSON once, in the HTML. The problem is that Angular (not me) is creating a new object every time the watcher looks at the value.

My suggestion is that Angular should check whether the attribute is JSON and if it is, either only process it once then store the JS object instead of the JSON string or throw an exception stating that it cannot handle JSON and how to get around it (at least this would save developers a lot of time).

@caitp
Copy link
Contributor

caitp commented Dec 6, 2013

http://plnkr.co/edit/VdSrFtVOj8u7ki3lP1Ef?p=preview (see script.js and web console), the thing is that although the $parse function is only created once, the populated object is actually created each time the getter is invoked.

This is typically not a problem, it's just something to be aware of

Oh, the $watcher is automatically removed if the parsed getter is a constant, as in this case... so it will only ever trigger the watch function once

https://github.com/angular/angular.js/blob/master/src/ng/rootScope.js#L337

@ghost ghost assigned tbosch Dec 9, 2013
@tbosch tbosch closed this as completed in 43072e3 Dec 13, 2013
jamesdaily pushed a commit to jamesdaily/angular.js that referenced this issue Jan 27, 2014
When a component uses an isolate scope reference
and the the component is used with an object literal
a new object is created on every evaluation.
Therefore the compiler needs to compare
the values of the parent and the isolate scope
using object equality and not object reference
equality.

Fixes angular#5296.
jamesdaily pushed a commit to jamesdaily/angular.js that referenced this issue Jan 27, 2014
When a component uses an isolate scope reference
and the the component is used with an object literal
a new object is created on every evaluation.
Therefore the compiler needs to compare
the values of the parent and the isolate scope
using object equality and not object reference
equality.

Fixes angular#5296.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.