ng-repeat looping over items twice?

4,722 views
Skip to first unread message

mike....@gmail.com

unread,
Nov 20, 2012, 12:33:21 AM11/20/12
to ang...@googlegroups.com
I'm new to angular so this might be a dumb question, but why does ng-repeat iterate over every item twice? If you don't know what I'm talking about run this: http://plnkr.co/edit/dSmYH5 with the web console open.  There are 5 items but the helper function has been called 10 times.  Why is this necessary?

Peter Bacon Darwin

unread,
Nov 20, 2012, 6:49:27 AM11/20/12
to ang...@googlegroups.com
This is how angular works - it will run the ng-repeat directive as many times as it sees fit.
You are thinking too imperatively.  Do not put code to be repeated inside the template.  The template is declarative - it should describe what you want to see in the browser.
If you want to do processing on the data to be iterated over, do it in the controller before passing that data to the ng-repeat directive.
Pete

On 20 November 2012 05:33, <mike....@gmail.com> wrote:
I'm new to angular so this might be a dumb question, but why does ng-repeat iterate over every item twice? If you don't know what I'm talking about run this: http://plnkr.co/edit/dSmYH5 with the web console open.  There are 5 items but the helper function has been called 10 times.  Why is this necessary?

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en.
 
 

Nils Kilden-Pedersen

unread,
Nov 20, 2012, 10:15:43 AM11/20/12
to ang...@googlegroups.com
Doesn't answer the question of why it's necessary. It does seem, at the very least, to be an inefficient way to process. Perhaps it's necessary, but it would be good to know why and if it's intrinsic.

Pawel Kozlowski

unread,
Nov 20, 2012, 10:18:05 AM11/20/12
to ang...@googlegroups.com
Hi Nils!

This is the best explanation I know of:
http://stackoverflow.com/a/9693933/1418796

Cheers,
Pawel
Question? Send a fiddle
(http://jsfiddle.net/pkozlowski_opensource/Q2NpJ/) or a plunk
(http://plnkr.co/)
Need help with jsFiddle? Check this:
http://pkozlowskios.wordpress.com/2012/08/12/using-jsfiddle-with-angularjs/

Looking for UI widget library for AngularJS? Here you go:
http://angular-ui.github.com/

Nils Kilden-Pedersen

unread,
Nov 20, 2012, 10:36:20 AM11/20/12
to ang...@googlegroups.com
Ok, so essentially due to the two-way binding nature of angular, right?

If so, that makes sense.

Pawel Kozlowski

unread,
Nov 20, 2012, 10:41:00 AM11/20/12
to ang...@googlegroups.com
Hi!

Yes, basically AngularJS uses dirty-checking to figure out what (and
when) to repaint. Please note that this dirty checking might be
trigger by user's input (I guess this is what you mean by 2-way data
binding) as well as some async events ($timeout, $http calls etc.).

Another SO question that might explain why the whole data model must
be re-calculated: http://stackoverflow.com/q/12333410/1418796

Cheers,
Pawel
Reply all
Reply to author
Forward
0 new messages