I was working on an Angular 1.4 project recently, and it was taking a crazy long time to load. It was a truly single page application (no routes) that ran as a web page inside of a corporate CMS program. Basically, a large corp would give it's employees Ipads, and all of the company apps would be "subapps" inside of the corporate container app.
How to Get an Embarrassingly Long Load Time
Sometimes you want to stretch out the development time of your project so you can get more money (just kidding! That's pretty messed up!). Really, I'm just showing you this because I made this mistake, and I don't want you to make it and wondering why your app is so slow to load. What I was doing was creating controllers and putting async calls directly in the controller's constructor, with no use of $timeout. In my situation the app was taking about 12 seconds to load! It would first be a white screen. Then it would show a weird half-finished version of how the DOM should look, and then finally my ready-to-use app would be displayed in all it's glory. For a web page, this is unacceptably long, and I now I know how prevent async calls from holding up the DOM rendering.
Those Dag Nabbit Asyncronous Calls
Asynchronous calls take time. That's the whole point of using them. You call out to something- a REST api, loading from the file system, and interval goes off... You really don't want your entire DOM rendering to be held up by an async event call, and really it never needs to be. It's easy to make rookie mistakes in Angular, though, and this is a pretty common one (luckily, there's an easy fix!). So, I just put a random async call in my controller, and now my DOM is all whacked out when it loads. What should I do?
$timeout to the Rescue!
Thanks to a post by a SO user, parliament, I discovered that if you use the $timeout service with no delay value then it just occurs immediately, but after the SOM has rendered! If you're curious, you can find the original SO question here: http://stackoverflow.com/questions/11125078/is-there-a-post- render-callback-for-angular-js-directive)
So, in the end here's how I changed my code. This is the old code, the terribly bad and slow-loading version:
And here's the speedy new hotness:
It's pretty amazing, but putting your async calls in this $timeout block will allow the page to finish rendering which makes it seem much snappier and prevents weird partial-renderings. It's important to note that this doesn't speed up the actual loading of the JSON files, and since it will take some time after the DOM renders you will probably want to put a loading spinner or some other loading indicator in the empty area that will display the data in the JSON files (if you have a visible area like that, of course).
Don't Kick Yourself!
If you fell into this trap also, don't be ashamed. It's a little tricky, and wrapping your async calls in a $timeout with no delay does seem a bit hackish. The important thing is that know you know the right way to do it with the quickest load time; so don't forget it! :)
The posts on this site are written and maintained by Jim Lynch. About Jim...