2

I'm trying to write an ng-repeat with an array of objects, defining the array in the ng-repeat attribute, but for some reason it just repeats the output indefinitely.
I've boiled it down to the issue being to do with the last part; doing it inline.

<span ng-repeat="item in [{test:'a'}, {test:'b'}, {test:'c'}]">{{item.test}}</span>

result of above on my server

A bunch of Error: [$rootScope:infdig] error's turn up in the console.
Interestingly, although I'm running 1.2, no matter which angular version I choose in jsfiddle, it renders it just fine.

result of jsfiddle testing

But the console in jsfiddle is still flooded with errors Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!, doing this not only for arrays of objects, but arrays of arrays (test5).

My attempt at a workaround, test5 in that fiddle, saving the var, is really interesting because if you comment out the ng-repeat, it saves the scope variable fine without error.
Yet when you try to use that variable, that is where it dies. It's interesting because that is essentially test3, which goes off without a hitch.

So it must be an issue with the one thing I want to do, have the variable come from the html (instead of pre-existing in the js). So to kill the digest loop I tried switching the fiddle to v1.4 to use one time binding and it still fails (console error wise).

Other people seem to have hit the issue, but fixed it in their own library instead of reporting it upstream

Is there any way for me to get this working properly?

2
  • do you call $evalAsync() or a similar function in your ng-repeat? Commented Jun 1, 2016 at 10:50
  • nope. The fiddles are right there, and that code snippet [at the start of the question] is all you need to see it happening. The only reason it displays 'right' in the fiddle is because the digest depth is capped so it errors right after the first iteration Commented Jun 1, 2016 at 10:55

1 Answer 1

2

Have a look at this code.

<!--what I actually want to work-->
  <span ng-init="test10 = [{'text':'but'}, {'text':'this'}, {'text':'refuses'}, {'text':'to'}, {'text':'work'}]"></span>
  <span ng-repeat="item in test10">{{item.text}} </span>

With ng-init you can set a scope variable from the view.

Hope it helps!

Sign up to request clarification or add additional context in comments.

1 Comment

I can't believe I forgot about init, that's delicious. Thanks, works perfectly. Sucks that you can't put the ng-init in the same div, but at least I can make it a little cleaner as it allows multiple declarations per init

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.