If $timeout(..., 0) works in your case, then I agree that it's OK to use, but I still think $watch() is the more idiomatic way of doing this. Presumably in your finished application the data won't be hardcoded into the controller; if it's coming from an asynchronous HTTP request, then you'll have to set the timeout delay to some hopefully-long-enough-but-not-so-long-it-slows-down-the-UI number of milliseconds. With $watch(), Angular will take care of notifying your code when the data is loaded and the variables are populated.
Basically, I think the rule of thumb is that using $timeout with a 0 delay to wait for the digest cycle to finish is fine, whereas using it with a fudge-factor value to wait for something asynchronous should be considered a hack, albeit one that's sometimes necessary when dealing with libraries external to Angular. Kevin On Wed, Aug 5, 2015 at 3:42 AM, Sander Elias <[email protected]> wrote: > Hi Andrew, > >> Am I correct in assuming that the reason $timeout works is because it >> puts the closure at the end of the event loop queue so that all of the >> angular binding functions have fired before the $timeout function is >> executed? >> > That is indeed the case. It makes sure there is at least 1 $digest loop > <https://www.ng-book.com/p/The-Digest-Loop-and-apply/> finished. > > Regards > Sander > > -- > You received this message because you are subscribed to the Google Groups > "AngularJS" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/angular. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "AngularJS" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/angular. For more options, visit https://groups.google.com/d/optout.
