<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>AngularJS on dwmkerr.com</title><link>https://dwmkerr.com/categories/angularjs/</link><description>Recent content in AngularJS on dwmkerr.com</description><generator>Hugo -- gohugo.io</generator><language>en-uk</language><managingEditor>Dave Kerr</managingEditor><copyright>Copright &amp;copy; Dave Kerr</copyright><lastBuildDate>Mon, 25 Apr 2016 09:45:00 +0000</lastBuildDate><atom:link href="https://dwmkerr.com/categories/angularjs/index.xml" rel="self" type="application/rss+xml"/><item><title>Moving from React + Redux to Angular 2</title><link>https://dwmkerr.com/moving-from-react-redux-to-angular-2/</link><pubDate>Mon, 25 Apr 2016 09:45:00 +0000</pubDate><guid>https://dwmkerr.com/moving-from-react-redux-to-angular-2/</guid><description>&lt;p&gt;I&amp;rsquo;ve just finished working on a very large project written in React and Redux. The whole team were new to both and we loved them.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;m going to share my experiences of experimenting in Angular 2 with you, from the point of view of someone who needs a pretty compelling reason to move away from my JSX and reducers.&lt;/p&gt;
&lt;h1 id="the-journey-so-far"&gt;The Journey So Far&lt;/h1&gt;
&lt;p&gt;Let me highlight a few key moments in my UI development experiences, to give a bit of context to my ramblings.&lt;/p&gt;
&lt;p&gt;&lt;img src="images/Journey.jpg" alt="The Journey So Far"&gt;&lt;/p&gt;
&lt;p&gt;Reading about redux was a lightbulb moment for me - rather than a complex framework it&amp;rsquo;s a simply library to help apply a few common sense functional programming principles - state is immutable, functions apply predictable transformations to data to produce new data.&lt;/p&gt;
&lt;p&gt;Learning React took a little bit of getting used to, but not too much, it was quite a bit more simple than Angular anyway.&lt;/p&gt;
&lt;p&gt;Long story short, simple React components and rigorous state management has so far resulted in the most manageable and well written very large scale UIs I&amp;rsquo;ve worked on so far - can Angular 2 compete with this?&lt;/p&gt;
&lt;h1 id="first-step-with-angular-2---folder-structure-typescript-sublime-text"&gt;First Step with Angular 2 - Folder Structure, Typescript, Sublime Text&lt;/h1&gt;
&lt;p&gt;I checked out &lt;a href="https://angular.io/docs/ts/latest/quickstart.html"&gt;the pretty neat &amp;lsquo;Getting Started&amp;rsquo; guide from Angular&lt;/a&gt; which promised to get me started in five minutes.&lt;/p&gt;
&lt;p&gt;It didn&amp;rsquo;t take five minutes, there&amp;rsquo;s a few gotchas, so I&amp;rsquo;m going to give a condensed guide here.&lt;/p&gt;
&lt;h2 id="step-1-the-folder-structure"&gt;Step 1: The Folder Structure&lt;/h2&gt;
&lt;p&gt;The first few steps of the angular guide creates the following folder structure:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;|-- angular2-starter
|-- tsconfig.json
|-- typings.json
|-- package.json
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This is the standard &lt;code&gt;package.json&lt;/code&gt; with some scripts ready to go. We also have &lt;code&gt;tsconfig.json&lt;/code&gt; to configure the typescript compiler and &lt;code&gt;typings.json&lt;/code&gt; to provide info to the compiler on where to get type information.&lt;/p&gt;
&lt;p&gt;You can check the code at this stage here:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://github.com/dwmkerr/angular2-starter/tree/step1"&gt;https://github.com/dwmkerr/angular2-starter/tree/step1&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src="images/Step1.png" alt="Step 1 GitHub Screenshot"&gt;&lt;/p&gt;
&lt;h2 id="node--npm-issues"&gt;Node &amp;amp; NPM Issues&lt;/h2&gt;
&lt;p&gt;At this stage the quickstart says you can run &lt;code&gt;npm install&lt;/code&gt; and all will be well:&lt;/p&gt;
&lt;p&gt;&lt;img src="images/npm-install.png" alt="npm install screenshot"&gt;&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;npm ERR! cb() never called!
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Not so good! For the record I&amp;rsquo;m using NPM 3.7.3 installed via homebrew. This looks like a bug in Beta 15 (see &lt;a href="https://github.com/angular/angular/issues/8053"&gt;Issue #8053&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;I fixed this by using &lt;em&gt;n&lt;/em&gt; to upgrade my node version:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;$ node -v
v5.9.0
$ npm install -g n # install &amp;#39;n&amp;#39; node version manager
$ sudo n latest
installed : v5.11.0
$ node -v
v5.11.0
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Now it &lt;code&gt;npm install&lt;/code&gt; runs OK.&lt;/p&gt;
&lt;h2 id="step-2-adding-components-and-configuring-sublime"&gt;Step 2: Adding Components and Configuring Sublime&lt;/h2&gt;
&lt;p&gt;The next steps of the walkthrough take us through adding an app component, a &lt;code&gt;main.ts&lt;/code&gt; file to bootstrap the application and an index file. You can check the updates here:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://github.com/dwmkerr/angular2-starter/tree/step2"&gt;https://github.com/dwmkerr/angular2-starter/tree/step2&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Essentially we now have:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;|-- angular2-starter
|-- tsconfig.json
|-- typings.json
|-- package.json
|-- index.html
|-- styles.css
|-- app
|-- main.ts
|-- app.component.ts
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;At this stage, running &lt;code&gt;npm start&lt;/code&gt; gives us a browerserified app to play with:&lt;/p&gt;
&lt;p&gt;&lt;img src="images/Step2.png" alt="Step 2 Screenshot"&gt;&lt;/p&gt;
&lt;p&gt;Clear enough so far, although the code in Sublime is not looking so pretty:&lt;/p&gt;
&lt;p&gt;&lt;img src="images/Step2Sublime.png" alt="Step 2 Sublime Text Screenshot"&gt;&lt;/p&gt;
&lt;p&gt;Quickly installing the &lt;a href="https://github.com/Microsoft/TypeScript-Sublime-Plugin"&gt;TypeScript plugin&lt;/a&gt; from Microsoft[^n] seems to do the trick:&lt;/p&gt;
&lt;p&gt;&lt;img src="images/Step2SublimeFormatted.png" alt="Step 2 Sublime Text with TypeScript plugin"&gt;&lt;/p&gt;
&lt;p&gt;If you need more details, here&amp;rsquo;s a gist with the full setup for Sublime 3, assuming you&amp;rsquo;ve got nothing installed.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://gist.github.com/dwmkerr/04fa8b8c15d049d0381e7798a79bcc45"&gt;https://gist.github.com/dwmkerr/04fa8b8c15d049d0381e7798a79bcc45&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;At this stage the app will run, we can see the basics of the Angular 2 syntax and start experimenting.&lt;/p&gt;
&lt;h2 id="step-3-adding-some-components"&gt;Step 3: Adding some components&lt;/h2&gt;
&lt;p&gt;At this stage the quick started guide starts going into more detail, guiding you through the process of creating multiple components. I decided to go off on my own here, with the rough plan of being able to write a set of goals for the day and turn it into a check-list[^n].&lt;/p&gt;
&lt;p&gt;Within not much time I had the some basic components, input and output, bindings and so on. Some screenshots:&lt;/p&gt;
&lt;p&gt;&lt;img src="images/Goals-Screenshot-1.png" alt="Goals Screenshot 1"&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src="images/Goals-Screenshot-2-1.png" alt="Goals Screenshot 2"&gt;&lt;/p&gt;
&lt;p&gt;You can take a look at the code at this stage by checking out the &amp;lsquo;step3&amp;rsquo; branch:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://github.com/dwmkerr/angular2-starter/tree/step3"&gt;github.com/dwmkerr/angular2-starter/tree/step3&lt;/a&gt;&lt;/p&gt;
&lt;h1 id="thoughts-so-far"&gt;Thoughts so far&lt;/h1&gt;
&lt;p&gt;For now, that&amp;rsquo;s all I&amp;rsquo;ve got time for. I&amp;rsquo;ve had a chance to get a feel for Angular 2, I&amp;rsquo;m going to come back to this in a few weeks and integrate Redux, maybe swap out System.JS for Webpack and do some experimenting.&lt;/p&gt;
&lt;p&gt;Opinions[^n] so far?&lt;/p&gt;
&lt;h3 id="not-sold-on-typescript"&gt;Not Sold on TypeScript&lt;/h3&gt;
&lt;p&gt;I&amp;rsquo;ve used TypeScript in my mess around, rather than plain &amp;lsquo;ol JavaScript, to keep the experience authentic to the angular team&amp;rsquo;s goals of using TypeScript to help.&lt;/p&gt;
&lt;p&gt;So far, I&amp;rsquo;m not seeing an enormous benefit. Some of the extra information available to auto-completion in nice, but this is a tooling thing.&lt;/p&gt;
&lt;p&gt;JavaScript is not a static language, the TypeScript annotations I find slowing me down a little.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;There&amp;rsquo;s so much extra domain specific &lt;em&gt;stuff&lt;/em&gt; in Angular 2 that people might be lost without it. But if your stuff is so complex you need to adapt the base language, is it &lt;strong&gt;too&lt;/strong&gt; complex?&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3 id="explicit-component-surface-areas-are-a-nice-idea"&gt;Explicit Component Surface Areas are a Nice Idea&lt;/h3&gt;
&lt;p&gt;When defining a component, you specify explicitly what comes &lt;em&gt;in&lt;/em&gt; (data) and what goes &lt;em&gt;out&lt;/em&gt; (events).&lt;/p&gt;
&lt;p&gt;This means that the surface area of a component (i.e. the part you touch if you interact with it programmatically) is well defined. This is a good thing.&lt;/p&gt;
&lt;p&gt;However, this is all handled with some pretty framework-specific stuff[^n]:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// e.g.
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;export&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;class&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;GoalsBoxComponent&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// Event we fire when the goals change.
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#960050;background-color:#1e0010"&gt;@&lt;/span&gt;&lt;span style="color:#a6e22e"&gt;Output&lt;/span&gt;() &lt;span style="color:#a6e22e"&gt;goalsChanged&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;EventEmitter&lt;/span&gt;&lt;span style="color:#f92672"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a6e22e"&gt;Goal&lt;/span&gt;[]&lt;span style="color:#f92672"&gt;&amp;gt;&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;new&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;EventEmitter&lt;/span&gt;();
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// e.g.
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;export&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;class&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;GoalListComponent&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// Input is a set of goals to render.
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#960050;background-color:#1e0010"&gt;@&lt;/span&gt;&lt;span style="color:#a6e22e"&gt;Input&lt;/span&gt;() &lt;span style="color:#a6e22e"&gt;goals&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;Goal&lt;/span&gt;[] &lt;span style="color:#f92672"&gt;=&lt;/span&gt; [];
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In a nutshell&amp;hellip;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Explicit component surface area is a cool idea.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;React does this too with the optional &lt;code&gt;propTypes&lt;/code&gt;, but it is not enforced. &lt;em&gt;However&lt;/em&gt;, how this is done in Angular has already gone through a few radical changes with some &lt;a href="https://github.com/angular/angular/pull/4435#issuecomment-144789359"&gt;lively debate&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id="not-ready-for-production-yet"&gt;Not ready for production&amp;hellip; yet&lt;/h3&gt;
&lt;p&gt;There&amp;rsquo;s no standardised, documented way to test a component - nuff said. But things are evolving quickly.&lt;/p&gt;
&lt;h3 id="framework-fatigue"&gt;Framework Fatigue&lt;/h3&gt;
&lt;p&gt;Comparing React to Angular is unfair, one is a view library, one is a framework. But it&amp;rsquo;s worth pointing out this is a pretty complex framework. There&amp;rsquo;s a &lt;strong&gt;lot&lt;/strong&gt; of very domain specific stuff. See this documentation for an example:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#f92672"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a6e22e"&gt;li&lt;/span&gt; &lt;span style="color:#f92672"&gt;*&lt;/span&gt;&lt;span style="color:#a6e22e"&gt;ngFor&lt;/span&gt;&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#34;#hero of heroes&amp;#34;&lt;/span&gt;&lt;span style="color:#f92672"&gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;From &lt;a href="https://angular.io/docs/ts/latest/tutorial/toh-pt2.html"&gt;the documentation&lt;/a&gt;:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The (*) prefix to ngFor indicates that the &lt;code&gt;&amp;lt;li&amp;gt;&lt;/code&gt; element and its children constitute a master template.&lt;/p&gt;
&lt;p&gt;&amp;hellip;&lt;/p&gt;
&lt;p&gt;The # prefix before &amp;ldquo;hero&amp;rdquo; identifies the hero as a local template variable. We can reference this variable within the template to access a hero’s properties.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;You&amp;rsquo;ll get used to it (if you have to), but I think it&amp;rsquo;s harder to &lt;em&gt;reason&lt;/em&gt; about than:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;render&lt;/span&gt; () {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; (
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#f92672"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a6e22e"&gt;div&lt;/span&gt;&lt;span style="color:#f92672"&gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&lt;span style="color:#66d9ef"&gt;this&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;props&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;goals&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;map&lt;/span&gt;((&lt;span style="color:#a6e22e"&gt;goal&lt;/span&gt;) =&amp;gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#f92672"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a6e22e"&gt;li&lt;/span&gt;&lt;span style="color:#f92672"&gt;&amp;gt;&lt;/span&gt;{&lt;span style="color:#a6e22e"&gt;goal&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;title&lt;/span&gt;}&lt;span style="color:#f92672"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#960050;background-color:#1e0010"&gt;/li&amp;gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&lt;span style="color:#f92672"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#960050;background-color:#1e0010"&gt;/div&amp;gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;OK fair enough, JSX is very specific, but the &lt;strong&gt;logic&lt;/strong&gt; (mapping an iterable) is JavaScript.&lt;/p&gt;
&lt;h1 id="wrapping-up"&gt;Wrapping Up&lt;/h1&gt;
&lt;p&gt;That&amp;rsquo;s it, for now. Next steps are to experiment more, see if it will play nice with Redux and share the next set of opinions.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;d love to hear what you think, so drop your comments below!&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Footnotes&lt;/strong&gt;&lt;/p&gt;</description><category>CodeProject</category></item><item><title>The Best Module System for AngularJS Applications</title><link>https://dwmkerr.com/the-best-module-system-for-angularjs-applications/</link><pubDate>Wed, 18 Mar 2015 14:47:10 +0000</pubDate><guid>https://dwmkerr.com/the-best-module-system-for-angularjs-applications/</guid><description>&lt;p&gt;I was working on a small and simple application built with AngularJS the other day. As with most applications like this, I start with a single JavaScript file caled &lt;code&gt;app.js&lt;/code&gt; and no module system.&lt;/p&gt;
&lt;p&gt;In the past I&amp;rsquo;ve used RequireJS with AngularJS. It&amp;rsquo;s an awful mistake. It leads to a big jump in complexity with no benefts. Angular apps don&amp;rsquo;t work well with AMDs, so really your are using RequireJS to combine files into one big file.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;m sure there&amp;rsquo;s a good analogy with hammers and nails. Something like:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;It&amp;rsquo;s like banging nails into your face with a hammer.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Maybe a bit extreme. But those who&amp;rsquo;ve used the two together may well be nodding sagely.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;ve also used Browserify. I prefer this approach, the syntax is cleaner. But it&amp;rsquo;s still a pain.&lt;/p&gt;
&lt;p&gt;Ideally, I&amp;rsquo;d like to use ECMA6 modules. So another approach is to just use ECMA6 module syntax and then compile your code with something like Traceur. But that requires quite a bit of tooling, slows down your pipeline and you&amp;rsquo;re still not &lt;em&gt;really&lt;/em&gt; using modules.&lt;/p&gt;
&lt;p&gt;I think the best approach is this one from &lt;a href="https://medium.com/@dickeyxxx"&gt;Jeff Dicky&lt;/a&gt; on his post &lt;a href="https://medium.com/@dickeyxxx/best-practices-for-building-angular-js-apps-266c1a4a6917"&gt;Best Practices for Building Angular.js Apps&lt;/a&gt;. Just forget all of the module stuff and concatenate only.&lt;/p&gt;
&lt;p&gt;Start with this:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;myproject
- app/
- css/
- vendor/
- index.html
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Or whatever your preferred structure is. Then stick your main file in &lt;code&gt;app/&lt;/code&gt;:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;myproject
- app/
- app.js
- css/
- vendor/
- index.html
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Your &lt;code&gt;app.js&lt;/code&gt; file should define your main Angular module:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;angular&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;module&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;app&amp;#39;&lt;/span&gt;, []);
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now just go ahead and concatenate everything in your &lt;code&gt;app/&lt;/code&gt; folder. Structure it however you want:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;myproject
- app/
- components/
- home/
- profile/
- app.js
- css/
- vendor/
- index.html
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Concat will put everything in the top level folder (i.e. &lt;code&gt;app.js&lt;/code&gt;) first. As long as you don&amp;rsquo;t put anything else in your top level folder (that comes before &amp;lsquo;a&amp;rsquo; alphabetically) then it doesn&amp;rsquo;t matter where you put your other files, as long as you define them without referencing any globals. So define your components like this:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;angular&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;module&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;app&amp;#39;&lt;/span&gt;).&lt;span style="color:#a6e22e"&gt;controller&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;SomeController&amp;#39;&lt;/span&gt;, &lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;() {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// something
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;});
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;No fuss no muss. No requires, no exports.&lt;/p&gt;
&lt;p&gt;If you need a new service, write it and save it. Same for directives or controllers or filters. Add the source file and it&amp;rsquo;s included, no messing around.&lt;/p&gt;
&lt;p&gt;Keep it simple, don&amp;rsquo;t force another module system on top of angular&amp;rsquo;s, you don&amp;rsquo;t get much benenfit. And wait patiently until ECMA6 moves more into the mainstream and we can start using native modules. There&amp;rsquo;s less and less point in investing in some super-sophisticated complex fancy module system for a framework which in vNext will throw it all away and for a language which will finally get native modules.&lt;/p&gt;
&lt;h3 id="words-for-gulpers"&gt;Words for Gulpers&lt;/h3&gt;
&lt;p&gt;If you are a gulp user, here&amp;rsquo;s how a pipeline might look to concat your JavaScript:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;var&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;gulp&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;require&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;gulp&amp;#39;&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;var&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;jshint&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;require&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;gulp-jshint&amp;#39;&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;var&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;stylish&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;require&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;jshint-stylish&amp;#39;&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;var&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;uglify&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;require&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;gulp-uglify&amp;#39;&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;var&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;rename&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;require&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;gulp-rename&amp;#39;&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;var&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;sourcemaps&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;require&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;gulp-sourcemaps&amp;#39;&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;var&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;concat&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;require&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;gulp-concat&amp;#39;&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;var&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;ngAnnotate&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;require&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;gulp-ng-annotate&amp;#39;&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// Hints and builds all JavaScript.
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;gulp&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;task&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;js&amp;#39;&lt;/span&gt;, &lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;() {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;gulp&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;src&lt;/span&gt;([&lt;span style="color:#e6db74"&gt;&amp;#39;./client/app/**/*.js&amp;#39;&lt;/span&gt;])
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; .&lt;span style="color:#a6e22e"&gt;pipe&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;jshint&lt;/span&gt;())
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; .&lt;span style="color:#a6e22e"&gt;pipe&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;jshint&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;reporter&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;stylish&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; .&lt;span style="color:#a6e22e"&gt;pipe&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;jshint&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;reporter&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;fail&amp;#39;&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; .&lt;span style="color:#a6e22e"&gt;pipe&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;sourcemaps&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;init&lt;/span&gt;({&lt;span style="color:#a6e22e"&gt;loadMaps&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;true&lt;/span&gt;}))
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; .&lt;span style="color:#a6e22e"&gt;pipe&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;concat&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;app.js&amp;#39;&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; .&lt;span style="color:#a6e22e"&gt;pipe&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;gulp&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;dest&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;./client/dist&amp;#39;&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; .&lt;span style="color:#a6e22e"&gt;pipe&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;ngAnnotate&lt;/span&gt;())
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; .&lt;span style="color:#a6e22e"&gt;pipe&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;uglify&lt;/span&gt;())
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; .&lt;span style="color:#a6e22e"&gt;pipe&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;rename&lt;/span&gt;({&lt;span style="color:#a6e22e"&gt;suffix&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;.min&amp;#39;&lt;/span&gt;}))
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; .&lt;span style="color:#a6e22e"&gt;pipe&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;sourcemaps&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;write&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;./&amp;#39;&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; .&lt;span style="color:#a6e22e"&gt;pipe&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;gulp&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;dest&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;./client/dist/&amp;#39;&lt;/span&gt;));
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;});
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Watch your app javascript folder and when it changes, you&amp;rsquo;ll hint everything, concat into a single distribution folder, annotate and uglify, as well as building full sourcemaps.&lt;/p&gt;
&lt;h3 id="what-about-other-stuff"&gt;What about other stuff?&lt;/h3&gt;
&lt;p&gt;For vendor code (jQuery, Bootstrap, whatever), don&amp;rsquo;t bother trying to be smart and require or import it. Just include it in your app with script tags. I wouldn&amp;rsquo;t go to the effort at trying to force some kind of smart module system on a language that doesn&amp;rsquo;t really support it - uf you can get away with avoiding it, do so.&lt;/p&gt;
&lt;p&gt;This is not an encouragement to be sloppy, this is just the easiest way to deal with the issue. The number of hours I&amp;rsquo;ve wasted tracking down &amp;lsquo;bugs&amp;rsquo; which were subtle issues to do with require.js or type-os has definitely made the approach above my preferred approach.&lt;/p&gt;</description><category>CodeProject</category></item><item><title>Fixing Memory Leaks in AngularJS and other JavaScript Applications</title><link>https://dwmkerr.com/fixing-memory-leaks-in-angularjs-applications/</link><pubDate>Tue, 03 Mar 2015 14:35:36 +0000</pubDate><guid>https://dwmkerr.com/fixing-memory-leaks-in-angularjs-applications/</guid><description>&lt;p&gt;Dealing with memory leaks in JavaScript applications can be a complex process. In this article I&amp;rsquo;m going to show you how to identify whether you have memory leaks, analyse them and ultimately resolve them.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;m using an AngularJS application to demonstrate the concepts and approaches, but much of this material applies to any JavaScript application.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href="#understandingmemoryleaks"&gt;Understanding Memory Leaks&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;What is a Memory Leak?&lt;/li&gt;
&lt;li&gt;Why is a Memory Leak Bad?&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="#identifyingmemoryleaks"&gt;Identifying Memory Leaks&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;Method 1: The Wrong Way&lt;/li&gt;
&lt;li&gt;Method 2: The Timeline&lt;/li&gt;
&lt;li&gt;Method 3: Recording Heap Allocations&lt;/li&gt;
&lt;li&gt;Method 4: Heap Snapshots&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="#analysingmemoryleaks"&gt;Analysing Memory Leaks&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;Analysing the leak in Scenario 2&lt;/li&gt;
&lt;li&gt;More on Graphs&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="#fixingmemoryleaks"&gt;Fixing Memory Leaks&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;Three golden rules&lt;/li&gt;
&lt;li&gt;Anti-patterns to avoid&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="#thefuture"&gt;The Future&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;Weak Maps&lt;/li&gt;
&lt;li&gt;AngularJS 2&lt;/li&gt;
&lt;li&gt;Even Better Browsers&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="#appendices"&gt;Appendices&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;Thanks&lt;/li&gt;
&lt;li&gt;Mysteries&lt;/li&gt;
&lt;li&gt;Futher Reading&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id="understanding-memory-leaks"&gt;Understanding Memory Leaks&lt;/h2&gt;
&lt;p&gt;If you&amp;rsquo;ve dealt with memory leaks before, or the patterns of memory usage we sometimes call memory leaks in memory managed applications, then you can probably skip to &lt;a href="#identifyingmemoryleaks"&gt;Identifying Memory Leaks&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;If not let&amp;rsquo;s start with some theory.&lt;/p&gt;
&lt;h3 id="what-is-a-memory-leak"&gt;What is a Memory Leak?&lt;/h3&gt;
&lt;p&gt;A memory leak, at least in the world of unmanaged applications, is what occurs when you allocate memory and forget to free it. In pseudo-code&lt;sup&gt;&lt;a href="#fn1" id="ref1"&gt;1&lt;/a&gt;&lt;/sup&gt;:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;void leaky()
{
void* memory;
memory = malloc(1000);
/* malloc just gave us some memory, use it! */
}
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;code&gt;memory&lt;/code&gt; will hold the address of the memory we&amp;rsquo;ve allocate. We use the memory, then the function ends. &lt;code&gt;memory&lt;/code&gt; goes out of scope and whatever address it held is lost - but we didn&amp;rsquo;t free the memory! Not only that, we&amp;rsquo;ve lost the address of it so can&amp;rsquo;t ever free it in the future - it&amp;rsquo;s &lt;em&gt;leaked&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;This memory is lost to the application - we can&amp;rsquo;t release it. Only terminating the process will release it back to the operating system.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;When we allocate memory and don&amp;rsquo;t release it when we are done, we have &amp;rsquo;leaked&amp;rsquo; that memory.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;So how do we get memory leaks in JavaScript applications? We don&amp;rsquo;t allocate memory directly, the engine does it for us, and it cleans it up afterwards as well&lt;sup&gt;&lt;a href="#fn2" id="ref2"&gt;2&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;
&lt;p&gt;If we hold on to objects longer than we need to, that will give us similar results. Let&amp;rsquo;s look at some code:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-javascript" data-lang="javascript"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;function&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;ChessManager&lt;/span&gt;() {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;this&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;moves&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; [];
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;this&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;makeMove&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;move&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;this&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;moves&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;push&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;move&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; };
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;this&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;newGame&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;() {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;moves&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;clear&lt;/span&gt;();
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; };
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Here we&amp;rsquo;ve got a bug - the &lt;code&gt;newGame&lt;/code&gt; function doesn&amp;rsquo;t clear the &lt;code&gt;ChessManager&lt;/code&gt;&amp;rsquo;s moves, it just throws a null reference exception. But we could use this class in our code. In theory if we keep on calling &lt;code&gt;makeMove&lt;/code&gt; we&amp;rsquo;ll just grow and grow the &lt;code&gt;moves&lt;/code&gt; array. This is a bug leading to memory that can&amp;rsquo;t be freed, even though we don&amp;rsquo;t need it.&lt;/p&gt;
&lt;p&gt;That&amp;rsquo;s a contrived example of a JavaScript memory leak.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;When we are finished with memory but don&amp;rsquo;t allow the garbage collector to clean it up, that&amp;rsquo;s a memory leak.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;At least for the purposes of this discussion. We&amp;rsquo;ll see it&amp;rsquo;s a very easy thing to do.&lt;/p&gt;
&lt;h3 id="why-is-a-memory-leak-bad"&gt;Why is a Memory Leak bad?&lt;/h3&gt;
&lt;p&gt;It might seem obvious but let&amp;rsquo;s just make sure we&amp;rsquo;re explicit with everything. As we said in the initial definition, we allocate memory but don&amp;rsquo;t deallocate it.&lt;/p&gt;
&lt;p&gt;In &lt;em&gt;some&lt;/em&gt; circumstances, this is not necessarily a disaster, if we don&amp;rsquo;t leak too much too often, but there are circumstances where this is very serious.&lt;/p&gt;
&lt;p&gt;Memory leaks cause performance problems, slow down applications and can lead to a process terminating. There are some times when that&amp;rsquo;s really not good.&lt;/p&gt;
&lt;p&gt;Servers and high performance applications shouldn&amp;rsquo;t leak, especially as many should be expected to run for long periods of time. Mobile apps or apps for embedded systems will need to deal with fewer resources and will suffer if they leak. Any application an end user is expecting to use for a long time will cause a lot of frustration if it leaks.&lt;/p&gt;
&lt;p&gt;That&amp;rsquo;s enough theory, let&amp;rsquo;s actually start looking at identifying memory leaks in the context of an AngularJS application.&lt;/p&gt;
&lt;h2 id="identifying-memory-leaks"&gt;Identifying Memory Leaks&lt;/h2&gt;
&lt;p&gt;I&amp;rsquo;ve created a sample app for showing photo albums which is leaky in parts. The app is at:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://dwmkerr.github.io/angular-memory-leaks/"&gt;dwmkerr.github.io/angular-memory-leaks&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;It&amp;rsquo;s a very basic app with a fairly common set of components; Bootstrap, jQuery and AngularJS. We&amp;rsquo;re going to take a look at how we can identify whether this app suffers from memory leaks.&lt;/p&gt;
&lt;p&gt;You can run the app in your browser, or run it locally with the commands:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;git clone https://github.com/dwmkerr/angular-memory-leaks.git
cd angular-memory-leaks
npm install &amp;amp;&amp;amp; bower install
gulp
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Running gulp will serve the app, lint, and reload the browser when you change the code. The project page is at &lt;a href="https://github.com/dwmkerr/angular-memory-leaks"&gt;github.com/dwmkerr/angular-memory-leaks&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id="method-1-the-wrong-way"&gt;Method 1: The Wrong Way&lt;/h3&gt;
&lt;p&gt;First, just be aware that the wrong way to look for leaks is by examing the memory usage of the Chrome process. While an increasing amount of memory usage &lt;em&gt;can&lt;/em&gt; indicate a leak, it is not reliable. Why?&lt;/p&gt;
&lt;p&gt;Well browsers can allocate memory and use it how they want to. A page it is rendering may no longer need as much memory as it needed before, but that doesn&amp;rsquo;t mean the browser needs to release it to the OS. It may just keep it to avoid having to re-allocate it later on.&lt;/p&gt;
&lt;h3 id="method-2-the-timeline"&gt;Method 2: The Timeline&lt;/h3&gt;
&lt;p&gt;Open the Chrome developer tools. Go to &amp;lsquo;Timeline&amp;rsquo; select &amp;lsquo;Memory&amp;rsquo; and hit &amp;lsquo;Record&amp;rsquo;.&lt;/p&gt;
&lt;p&gt;&lt;img src="images/StartRecording.png" alt="Start Recording"&gt;&lt;/p&gt;
&lt;p&gt;Now start using your application. After you are done, stop recording. You&amp;rsquo;ll see a graph of memory usage.&lt;/p&gt;
&lt;p&gt;&lt;img src="images/MemoryUsage.png" alt="Memory Usage"&gt;&lt;/p&gt;
&lt;p&gt;This is &lt;strong&gt;almost&lt;/strong&gt; exactly what we need. I&amp;rsquo;ll explain the almost shortly, but lets take a look at this graph.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;We see a &lt;em&gt;Used JS Heap&lt;/em&gt; in blue. &lt;em&gt;Used&lt;/em&gt; is important here - Chrome is telling us that there may be more heap usage than shown in its actual process, but what we are seeing here is what is actually used by the page.&lt;/li&gt;
&lt;li&gt;We see documents (in this case a steady value of one document).&lt;/li&gt;
&lt;li&gt;We see DOM nodes. As I use the app the nodes increase, up until a certain point and then they drop.&lt;/li&gt;
&lt;li&gt;We see Listeners (i.e. even handlers). Again, these increase as I use the app and then drop.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;So what should we be looking for in this graph? That depends on what our app is doing. But let&amp;rsquo;s imagine the we are navigating through different photo albums in the albums app. We&amp;rsquo;ll need more memory to see each album, but once we leave an album we don&amp;rsquo;t need that memory any more. So we should get a healthy saw-tooth pattern&lt;sup&gt;&lt;a href="#fn3" id="ref3"&gt;3&lt;/a&gt;&lt;/sup&gt;:&lt;/p&gt;
&lt;p&gt;&lt;img src="images/TimelineSawtooth.png" alt="Timeline Sawtooth"&gt;&lt;/p&gt;
&lt;p&gt;Here we see that we use more and more memory, up until the point that Chrome garbage collects, then goes back to where we started. This is repeated again and again. This is a good sign - when Chrome garbage collects we go back to the same place we started, a strong indication we are not leaking much memory.&lt;/p&gt;
&lt;p&gt;If we are doing some work which simply needs more and more memory, and we don&amp;rsquo;t release it, we would expect to see steps instead&lt;sup&gt;&lt;a href="#fn4" id="ref4"&gt;4&lt;/a&gt;&lt;/sup&gt;:&lt;/p&gt;
&lt;p&gt;&lt;img src="images/TimelineSteps-1.png" alt="Timeline Steps"&gt;&lt;/p&gt;
&lt;p&gt;An example of this might be an infinite scroll situation. I&amp;rsquo;m looking through a vast photo album, and when I get to the bottom of the screen I load more images automatically. The ones I&amp;rsquo;ve loaded are still in the DOM so cannot be released. We see no saw-tooth because there&amp;rsquo;s no release of memory. However, this is not a memory leak - it&amp;rsquo;s just increasing memory usage. It does mean that if we allow the user to scroll too much we may run out of resources though.&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;dangerous&lt;/strong&gt; case is the one below:&lt;/p&gt;
&lt;p&gt;&lt;img src="images/TimelineLeakySawtooth.png" alt="Leaky Sawtooth"&gt;&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s imaging we&amp;rsquo;re using the application, navigating through albums, returning the the home page, looking through some more albums and so on. We keep using memory, and Chrome keeps on garbage collecting, but we never quite get back to where we started. We are trending towards increasing memory usage. This indicates we &lt;em&gt;might&lt;/em&gt; be leaking memory.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Might&lt;/em&gt; is not going to cut the mustard, we need to know categorically what is going on and whether we have a leak.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;You said this is &amp;lsquo;almost&amp;rsquo; exactly what we need?&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Unfortunately, you cannot always trust this graph. See Mystery 1 for the ugly details. Suffice to say that what we&amp;rsquo;re seeing here is an indicator only, but for more detail we need to look at Method 3.&lt;/p&gt;
&lt;h3 id="method-3-recording-heap-allocations"&gt;Method 3: Recording Heap Allocations&lt;/h3&gt;
&lt;p&gt;Let&amp;rsquo;s look at a different way of seeing if we&amp;rsquo;ve got a leak, the &amp;lsquo;Heap Allocations&amp;rsquo; view. In the developer tools, go to &amp;lsquo;Profiles&amp;rsquo; and &amp;lsquo;Record Heap Allocations&amp;rsquo;:&lt;/p&gt;
&lt;p&gt;&lt;img src="images/HeapAllocations.png" alt="Record Heap Allocations"&gt;&lt;/p&gt;
&lt;p&gt;When we record heap allocations we get a chart showing us spikes as we allocate memory. These spikes are initially blue (meaning Chrome is using the memory), then change to grey once the memory is freed. If we see spikes or sections of spikes that remain blue, we may have a problem.&lt;/p&gt;
&lt;p&gt;Try this, go to the Ablums app and start recording. Click on the &amp;lsquo;India&amp;rsquo; album, then go back to the home page. You should see a chart like this:&lt;/p&gt;
&lt;p&gt;&lt;img src="images/HeapAllocationsEx1.png" alt="Heap Allocations Example 1"&gt;&lt;/p&gt;
&lt;p&gt;So we start recording and nothing is being allocated. Then we click on the &amp;lsquo;India&amp;rsquo; album (point 1) and we get a few spikes, as chrome allocates memory needed for the content in the new page. Then we click back on the home page (point 2). Some of the memory used in the India album is released (it looks like about half). One spike of memory used for the home page is still in use (what we&amp;rsquo;d expect) and another spike or two seem to be freed. These other spikes might be memory used for the actual transition, for example in logic in the router.&lt;/p&gt;
&lt;p&gt;So this looks like we may have a problem in the album page. In fact, we can drag a selection box around those first three spikes and see what is &lt;em&gt;still&lt;/em&gt; in memory (i.e. what might be a potential leak) in the view below:&lt;/p&gt;
&lt;p&gt;&lt;img src="images/HeapAllocationsEx2.png" alt="Heap Allocations Example 2"&gt;&lt;/p&gt;
&lt;p&gt;Dissecting this view we have:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;A subset of the data, the blue spike from the album page which is still in use.&lt;/li&gt;
&lt;li&gt;The &amp;lsquo;Heap View&amp;rsquo;, which shows us different &lt;em&gt;types&lt;/em&gt; of data in memory. Don&amp;rsquo;t worry, we&amp;rsquo;ll see a lot more on this later.&lt;/li&gt;
&lt;li&gt;An instance of a specific type of data, in this case an instance of a JavaScript object.&lt;/li&gt;
&lt;li&gt;The retainers graph for the specific object.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;We&amp;rsquo;re going to look into what all of this means in a lot of detail as we go through the article. For now, I&amp;rsquo;ll simply state what we&amp;rsquo;re seeing, by the end of the article you&amp;rsquo;ll be able to analyse this (and much more) yourself.&lt;/p&gt;
&lt;p&gt;In this snapshot we see a small amount of data still in use. A quick look through the data reveils we have data still in use which relats to the AngularJS template cache.&lt;/p&gt;
&lt;p&gt;This is good! It means this is probably not a leak. When I first visit the album page AngularJS is caching the template used to render it, so of course it stays in memory.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;When analysing memory usage remember that caching, preloading and other optimisation techniques may cause some noise.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;So if we have the albums page in a cache, in theory the next time we visit the page and then return to the home page, we should free a lot more of the memory (because the &lt;em&gt;new&lt;/em&gt; memory we allocate will be just for the page itself, not the cache which is already set up). Let&amp;rsquo;s try it. We&amp;rsquo;ll record going to the album page, back to the homepage, then the album page and back again:&lt;/p&gt;
&lt;p&gt;&lt;img src="images/HeapAllocationsEx3.png" alt="Heap Allocations Example 3"&gt;&lt;/p&gt;
&lt;p&gt;This is looking good.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;We go to the &amp;lsquo;India&amp;rsquo; album. Some memory used is now freed, but much is still in use. As we saw, at least some of that is the template cache.&lt;/li&gt;
&lt;li&gt;We go back to the home page, lots of memory is used but by the time we&amp;rsquo;re done recording it&amp;rsquo;s almost entirely freed.&lt;/li&gt;
&lt;li&gt;We visit the India album a second time, requiring some memory almost all of which is freed.&lt;/li&gt;
&lt;li&gt;We go back to the home page. Some memory is used during the transition and to render the page, some of that is still in use (which is expected as the page is still open).&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The heap allocations chart is exceptionally useful in identifying memory leaks, it has already led to some insights:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Initial loading of pages increases our &amp;lsquo;baseline&amp;rsquo; memory footprint due to data being added to caches (such as the AngularJS template cache).&lt;/li&gt;
&lt;li&gt;Subsequent loading of pages requires memory, but the vast majority of it is freed.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;One thing we noticed from this brief analysis was that the initial result was slightly misleading. With the heap allocations view repeated operations can help you identify trends. In the Albums application I&amp;rsquo;ve actually set up part of the app to run repeated operations, so we can try to consistently test scenarions. The &amp;lsquo;scenarios&amp;rsquo; menu lets us run them. Let&amp;rsquo;s try running scenario 1.&lt;/p&gt;
&lt;p&gt;&lt;img src="images/Scenario1.png" alt="Scenario 1"&gt;&lt;/p&gt;
&lt;p&gt;This scenario will navigate from &lt;code&gt;/&lt;/code&gt; (the home page) to &lt;code&gt;/nowhere&lt;/code&gt; ten times. &lt;code&gt;/nowhere&lt;/code&gt; isn&amp;rsquo;t matched by the router so takes us back to the home page. This has the effect of reloading the home page 20 times (just reloading doesn&amp;rsquo;t work, the router is smart enough to realise we&amp;rsquo;re staying on the same page).&lt;/p&gt;
&lt;p&gt;&lt;img src="images/Scneario1HeapAllocations.png" alt="Scenario 1 Heap Allocations"&gt;&lt;/p&gt;
&lt;p&gt;While you are recording the chart you can see peaks go from blue to grey as memory is freed. Let&amp;rsquo;s see what we&amp;rsquo;ve got.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Shows our first navigation, some memory is not freed. Everything before this is setup code.&lt;/li&gt;
&lt;li&gt;Our last navigation. Some memory still in use (as expected).&lt;/li&gt;
&lt;li&gt;A glance at memory in use shows some compiled code and system data (more on this later). At this stage we don&amp;rsquo;t need to worry, Chrome will allocate data like this when it needs to.&lt;/li&gt;
&lt;li&gt;It looks like the 11th page load didn&amp;rsquo;t free all of it&amp;rsquo;s memory. This is potential cause for worry.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Altogether this a very healthy looking scenario. The huge majority of what we allocate is freed, as we would hope. Small amounts of memory stay in use (mostly used under the hood by Chrome) and a small amount of memory after the 11th reload is not freed (a quick look suggests a timing issue, definitely something we&amp;rsquo;d want to investigate further in a real-world app). Our allocations are in the 50 KB to 100 KB range and we&amp;rsquo;re looking good.&lt;/p&gt;
&lt;p&gt;Before we say goodbye to the Heap Allocations view (for now) let&amp;rsquo;s do the same for Scenario 2 (moving from the home page to the top rated page 10 times).&lt;/p&gt;
&lt;p&gt;&lt;img src="images/Scenario2HeapAllocations.png" alt="Scenario 2 Heap Allocations"&gt;&lt;/p&gt;
&lt;p&gt;We are not going to analyse this issue (yet!) but this is an example of a much less healthy chart. In this chart we seem to be allocating memory for each page view and not releasing it. This kind of chart definitely indicates that there could be problems.&lt;/p&gt;
&lt;p&gt;So we&amp;rsquo;ve seen the Heap Allocations view, which is a bit more sophisticated than the memory usage graph. Let&amp;rsquo;s look at the last way to analyse memory leaks - snapshots.&lt;/p&gt;
&lt;h3 id="method-4-heap-snapshots"&gt;Method 4: Heap Snapshots&lt;/h3&gt;
&lt;p&gt;The final method of identifying memory leaks is the most sophisticated and finely controlled. We will take snapshots at specific points in time and analyse the differences between them. To take a snapshot, we go to the Profiles view and choose &amp;lsquo;Take Heap Snapshot&amp;rsquo;:&lt;/p&gt;
&lt;p&gt;&lt;img src="images/TakeHeapSnapshot.png" alt="Take Heap Snapshot"&gt;&lt;/p&gt;
&lt;p&gt;When we take a heap snapshot Chrome simply records the details of all memory allocated.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Remember: Taking a Snapshot &lt;strong&gt;always&lt;/strong&gt; runs garbage collection first.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;A heap snapshot shows you exactly the same kind of data you get in the Heap Allocations view, except that you are seeing ALL memory in use, not just objects which were allocated and are still alive:&lt;/p&gt;
&lt;p&gt;&lt;img src="images/HeapSnapshot1.png" alt="A Heap Snapshot"&gt;&lt;/p&gt;
&lt;p&gt;This view is very complete but not necessarily very useful. There&amp;rsquo;s some extra ways to see the data (if you change from &amp;lsquo;Summary&amp;rsquo; to another view or change &amp;lsquo;All Objects&amp;rsquo; but we&amp;rsquo;ll see that later).&lt;/p&gt;
&lt;p&gt;Staying on topic, we&amp;rsquo;ll not yet look in detail at what the data is that we are seeing, we&amp;rsquo;ll first look into identifying whether there are memory leaks - then we&amp;rsquo;ll look into tracking them down.&lt;/p&gt;
&lt;p&gt;Indivdiual snapshots are not so helpful for checking for leaks, but what is very helpful is the ability to compare memory used between snapshots.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s take some snapshots, try this:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Open the app.&lt;/li&gt;
&lt;li&gt;Navigate to the top rated page (caches should now be set up).&lt;/li&gt;
&lt;li&gt;Navigate to the home page. Take a snapshot.&lt;/li&gt;
&lt;li&gt;Navigate to the top rated page. Take a snapshot.&lt;/li&gt;
&lt;li&gt;Navigate to the home page. Take a snapshot.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Now we can do something really cool. Select snapshot 3, and choose to view data allocated between snapshot 1 and 2. This means we&amp;rsquo;re seeing data allocated for the top rated page, which is &lt;em&gt;still&lt;/em&gt; in use when we go back to the home page, i.e. probably leaked.&lt;/p&gt;
&lt;p&gt;&lt;img src="images/SnapshotComparison.png" alt="Snapshot Comparison"&gt;&lt;/p&gt;
&lt;p&gt;So what are we seeing now?&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;We have three snapshots. The size of each one is shown. &lt;em&gt;Sometimes&lt;/em&gt; the very first one seems overly high. See Mystery 2. We have selected the 3rd snapshot and are therefore only able to see data still present in this snapshot.&lt;/li&gt;
&lt;li&gt;We are chosing to show only objects allocated between Snapshot 1 and 2, i.e. objects allocated to present the page. But we&amp;rsquo;re &lt;strong&gt;in&lt;/strong&gt; snapshot 3, so we&amp;rsquo;re seeing those objects which were allocated and are still present.&lt;/li&gt;
&lt;li&gt;Objects allocated are looking suspicious - we&amp;rsquo;ve got DOM elements. This doesn&amp;rsquo;t look good!&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;This is the best way to identify memory leaks. So now that we&amp;rsquo;ve seen how to identify whether we have memory leaks, or at least that we have a potential problem to analyse we can move onto step 2 - Analysing Memory Leaks.&lt;/p&gt;
&lt;h2 id="analysing-memory-leaks"&gt;Analysing Memory Leaks&lt;/h2&gt;
&lt;p&gt;If we think we have a memory leak, we need to be able to look at the heap data and see what&amp;rsquo;s going on. Whether we are seeing heap data from a selection of allocations from the Heap Allocations view or from the Heap Snapshots, we see the same kind of information:&lt;/p&gt;
&lt;p&gt;&lt;img src="images/HeapData.png" alt="Heap Data"&gt;&lt;/p&gt;
&lt;p&gt;Starting from the left we have the &amp;lsquo;Constructor&amp;rsquo; column. This is the type of object we have. Some of these objects we can see are JavaScript classes (constructed with a &lt;code&gt;new&lt;/code&gt; call to a function), such as &lt;code&gt;Scope&lt;/code&gt;. As well as our own classes, we have some special classes of data:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;(compiled code): Represents JavaScript code compiled by Chrome. Consider this internal - we have no control over it.&lt;/li&gt;
&lt;li&gt;(array): Internally used array object. Again, internal.&lt;/li&gt;
&lt;li&gt;Array: A JavaScript array. Often we have a &lt;em&gt;lot&lt;/em&gt; of data in arrays.&lt;/li&gt;
&lt;li&gt;Object: A plain old JavaScript object.&lt;/li&gt;
&lt;li&gt;(closure): A closure.&lt;/li&gt;
&lt;li&gt;system / Context: The underlying data require to call a function, for example the actual data used by a closure.&lt;/li&gt;
&lt;li&gt;system: Internally used data.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;There are also plenty of objects that are created by Chrome, such as &lt;code&gt;HTMLDivElement&lt;/code&gt;, which is a wrapper around the internally used (native) DOM object.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s dissect some of these objects in detail. Running &lt;strong&gt;Scenario 3&lt;/strong&gt; allocates some data and puts it on the &lt;code&gt;window&lt;/code&gt; object. This is really trivial data but shows a lot. You can use the Heap Allocations View or Heap Snapshots to see the data. I&amp;rsquo;ve taken three snapshots (once before pressing OK, once after the data is allocated, and the final one when the last modal is closed):&lt;/p&gt;
&lt;p&gt;&lt;img src="images/HeapDataAnalysis2.png" alt="Heap Data Analysis Part 1"&gt;&lt;/p&gt;
&lt;p&gt;This data has come from the code below:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-javascript" data-lang="javascript"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// Create a class which will hold heap data. Makes it easier
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// to find the data in Chrome.
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;function&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;HeapData&lt;/span&gt;() {}
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// Create a heap data object.
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;var&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;heapData&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;new&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;HeapData&lt;/span&gt;();
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// Create a function that multiplies two numbers.
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;function&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;multiply&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;a&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;b&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;a&lt;/span&gt; &lt;span style="color:#f92672"&gt;*&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;b&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// Create a &amp;#39;multiply by&amp;#39; function, which curries the above
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// to generate a function which multiplies by a constant. This
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// will involve closures.
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;var&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;multiplyBy&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;a&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;b&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;multiply&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;a&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;b&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;};
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// Add some data to our heap data object.
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;heapData&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;fry&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;Philip J. Fry&amp;#34;&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;heapData&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;zoidberb&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;John &amp;#34;&lt;/span&gt; &lt;span style="color:#f92672"&gt;+&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;Zoidberg&amp;#34;&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;heapData&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;character&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;firstName&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;Amy&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;secondName&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;Wong&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;};
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;heapData&lt;/span&gt;.&lt;span style="color:#66d9ef"&gt;double&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;multiplyBy&lt;/span&gt;(&lt;span style="color:#ae81ff"&gt;2&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;heapData&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;multiplyBy100&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;multiplyBy&lt;/span&gt;(&lt;span style="color:#ae81ff"&gt;100&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;heapData&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;doubledNumber&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;heapData&lt;/span&gt;.&lt;span style="color:#66d9ef"&gt;double&lt;/span&gt;(&lt;span style="color:#ae81ff"&gt;18&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;heapData&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;multipliedNumber&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;heapData&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;multiplyBy100&lt;/span&gt;(&lt;span style="color:#ae81ff"&gt;15&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;heapData&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;div&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; document.&lt;span style="color:#a6e22e"&gt;createElement&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;div&amp;#34;&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// Put the heap data on the window, it is now pinned to a GC root.
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;window.&lt;span style="color:#a6e22e"&gt;heapData&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;heapData&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;We&amp;rsquo;ve got a little bit of everything here, some code, some closures, some objects and a DOM element.&lt;/p&gt;
&lt;p&gt;As we&amp;rsquo;ve put most of this data on the &lt;code&gt;heapData&lt;/code&gt; object, which is an instance of &lt;code&gt;HeapData&lt;/code&gt; we can easily find the object:&lt;/p&gt;
&lt;p&gt;&lt;img src="images/HeapDataAnalysis3.png" alt="Heap Data Analysis 3"&gt;&lt;/p&gt;
&lt;p&gt;So we can see the &lt;code&gt;HeapData&lt;/code&gt; constructor, expanding it we see an &lt;em&gt;instance&lt;/em&gt; of &lt;code&gt;HeapData&lt;/code&gt;. The &lt;code&gt;@420269&lt;/code&gt; is a unique ID assigned by Chrome. If we have lots of heap data objects, we can use this to distinguish between them when we&amp;rsquo;re looking at other parts of the snapshot. What else do we see?&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Distance&lt;/strong&gt;. How far the instance is from a GC Root. A GC root is anything that can &amp;lsquo;pin&amp;rsquo; objects, for example the &lt;code&gt;window&lt;/code&gt; object which holds globals. If put something on &lt;code&gt;window&lt;/code&gt; it will never be freed, this is what makes it a GC root. Our distance is 2 as we have &lt;code&gt;HeapData&lt;/code&gt; (constructor) to &lt;code&gt;heapData&lt;/code&gt; (instance) to &lt;code&gt;window&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Objects count&lt;/strong&gt;. Only valid for the top level nodes, this shows us how many objects of the specified type we have. We have 1 &lt;code&gt;HeapData&lt;/code&gt; object.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Shallow Size&lt;/strong&gt;. The size of the data that is directly allocated for the object. Compare this to &lt;em&gt;Retained Size&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Retained Size&lt;/strong&gt;. The size of data this object is retaining. For example, out &lt;code&gt;heapData&lt;/code&gt; instance holds a reference to an object which contains two fields &lt;code&gt;firstName&lt;/code&gt; and &lt;code&gt;secondName&lt;/code&gt;. Our shallow size includes enough data for the reference, the retained size includes the full retained size of the retained object.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Notice that our instance of &lt;code&gt;HeapData&lt;/code&gt; is highlighted in yellow? That&amp;rsquo;s a convenience from Chrome, it&amp;rsquo;s showing us objects which are &lt;strong&gt;directly accessible&lt;/strong&gt; from JavaScript. Our object can be accessed via &lt;code&gt;window.heapData&lt;/code&gt;, therefore it&amp;rsquo;s directly accessible. Other objects we&amp;rsquo;ve created might not be (for example, a variable used in a closure exists and is on the heap, but not directly accessible).&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s see some other data we allocated:&lt;/p&gt;
&lt;p&gt;&lt;img src="images/HeapDataAnalysis4-1.png" alt="Heap Data Analysis 4"&gt;&lt;/p&gt;
&lt;p&gt;Now we&amp;rsquo;re looking at closures. We have two closures in yellow next to each other, clicking on one shows the retainer graph. What is going on here?&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Our closure is not a simple thing. It has code (of course), which takes up memory. We won&amp;rsquo;t look into this in detail. It has shared function data (again, internally used and not worth looking into). We also have a reference to a &lt;code&gt;__proto__&lt;/code&gt; (a function object has a prototype!). Finally, we have the context, which contains enough data to call the function. If we look in to the context we will not see much, as our function contains numbers which Chrome can simply store in the code. However, if we use references in closures we&amp;rsquo;ll actually see them in the context.&lt;/li&gt;
&lt;li&gt;We also have the retainers. Our closure is referenced via a variable called &lt;code&gt;multiplyBy100&lt;/code&gt;, which itself is referenced by &lt;code&gt;heapData&lt;/code&gt;, which if referenced by the &lt;code&gt;window&lt;/code&gt; GC root.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;multiplyBy100&lt;/code&gt; variable is &lt;em&gt;also&lt;/em&gt; dominated by the second element of an array with id &lt;code&gt;@227339&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The last thing we&amp;rsquo;ll look at in this snapshot is the div element.&lt;/p&gt;
&lt;p&gt;&lt;img src="images/HeapDataAnalysis5.png" alt="Heap Data Analyis 5"&gt;&lt;/p&gt;
&lt;p&gt;We can see the div element is retained by the &lt;code&gt;div&lt;/code&gt; variable in the &lt;code&gt;heapData&lt;/code&gt; object. We can also see it is made up of a prototype and some native object. The native object shows no size - don&amp;rsquo;t be fooled. That just means its taking up no JavaScript heap memory. It is still using memory (just in V8 engine not the JavaScript code).&lt;/p&gt;
&lt;p&gt;What&amp;rsquo;s important to note here is that the element is shown in red. This means it&amp;rsquo;s &lt;strong&gt;detached&lt;/strong&gt;. So it exists, is referenced (and therefore cannot be garbage collected) but is not in the DOM. This is not necessarily a problem, but lots of detached DOM elements is often a bad sign, especially if the number is increasing.&lt;/p&gt;
&lt;p&gt;The rest of the data you can look through yourself. You&amp;rsquo;ll notice some interesting things, such as how concatenated strings work, but the important stuff we&amp;rsquo;ve now seen.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s move on to analyising the first potential memory leak we discovered - the transition to the Top Rated page of the albums app.&lt;/p&gt;
&lt;h3 id="analysing-the-leak-in-scenario-2"&gt;Analysing the leak in Scenario 2&lt;/h3&gt;
&lt;p&gt;We saw that &lt;strong&gt;Scenario 2&lt;/strong&gt; (switching to and from the &amp;rsquo;top rated&amp;rsquo; view) seemed to leak memory. Let&amp;rsquo;s use the heap snapshot comparison view to analyse this further. The steps are:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Navigate to the home page.&lt;/li&gt;
&lt;li&gt;Navigate to the top rated page (setting up the cache).&lt;/li&gt;
&lt;li&gt;Navigate to the home page, take a snapshot.&lt;/li&gt;
&lt;li&gt;Navigate to the top rated page, take a snapshot.&lt;/li&gt;
&lt;li&gt;Navigate to the home page, take a snapshot.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;We can now look at the memory allocated between 1 and 2 which is present in 3 (i.e. what we allocated for the top rated view and potentially leaked):&lt;/p&gt;
&lt;p&gt;&lt;img src="images/Scenario2Snapshot1.png" alt="Scenario 2 Snapshot 1"&gt;&lt;/p&gt;
&lt;p&gt;Some things jump out immediately:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;We have gone from 7.5 to 8.4 to 8.5 MB. We are changing from one view to another - and ending in the same place that we started. We &lt;strong&gt;should&lt;/strong&gt; be going back to 7.5 MB.&lt;/li&gt;
&lt;li&gt;We&amp;rsquo;ve got a lot of objects still hanging around, not just system data like compiled code, but HTML elements, detached DOM elements, &lt;code&gt;Promise&lt;/code&gt; objects, &lt;code&gt;n.fn.init&lt;/code&gt; objects and so on.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;This looks like a classic leak situation. Let&amp;rsquo;s start by looking at some objects we recognise. There are some &lt;code&gt;Scope&lt;/code&gt; objects near the top of the chart, let&amp;rsquo;s look at those.&lt;/p&gt;
&lt;p&gt;&lt;img src="images/Scenario1Part2.png" alt="Scenario 2 Part 2"&gt;&lt;/p&gt;
&lt;p&gt;We&amp;rsquo;ve got some &lt;code&gt;Scope&lt;/code&gt; objects, three in fact. These objects contain the usual AngularJS fields such as &lt;code&gt;$parent&lt;/code&gt;, the only field which distinguishes this scope is the &lt;code&gt;album&lt;/code&gt; field. If we look at out &lt;code&gt;aml-rated-album&lt;/code&gt; directive it looks like it could be the isolated scope for this directive:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-javascript" data-lang="javascript"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;.&lt;span style="color:#a6e22e"&gt;directive&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;amlRatedAlbum&amp;#39;&lt;/span&gt;, &lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;() {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;restrict&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;E&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;scope&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;album&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;=&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; } &lt;span style="color:#75715e"&gt;// etc
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This scope has an &lt;code&gt;album&lt;/code&gt; field. There are three albums so it looks likely these are the three albums in the top rated page, the scopes stil in memory. What retains them?&lt;/p&gt;
&lt;p&gt;Looking at the retainers (at &lt;strong&gt;2&lt;/strong&gt;) we don&amp;rsquo;t see much. We&amp;rsquo;re retained by a &lt;code&gt;$$ChildScope&lt;/code&gt;, which also retained by a &lt;code&gt;$$ChildScope&lt;/code&gt; object. In fact we have quite a complex graph of objects.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;When we leak a scope in AngularJS, we leak a huge graph of objects.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Scopes know about their parents. They also know about their children, and siblings. If we inadvertantly pin a scope to a GC root, we &lt;strong&gt;will probably leak almost all of the scopes in the page&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Why? The graph below should show why. I &amp;rsquo;leak&amp;rsquo; a scope, and by doing so I retain all of the other scopes, because they are connected. Having a connected graph of scopes is required for angular to work, but it means that we we are extremely susceptible to leaking a &lt;strong&gt;lot&lt;/strong&gt; of data.&lt;/p&gt;
&lt;p&gt;&lt;img src="images/ScopeLeakGraph1.png" alt="Scope leak graph"&gt;&lt;/p&gt;
&lt;p&gt;This graph shows &lt;code&gt;$parent&lt;/code&gt; retained relationships, but don&amp;rsquo;t forget scopes also know about their children and their siblings, so real graph is even more highly connected.&lt;/p&gt;
&lt;p&gt;So just grabbing a specific scope is not good enough. We need to try and be a little bit more specific. Let&amp;rsquo;s try starting from an element instead. Here we take a look at a div element and its retainers:&lt;/p&gt;
&lt;p&gt;&lt;img src="images/Scenario2Part3.png" alt="Scenario 2 Part 3"&gt;&lt;/p&gt;
&lt;p&gt;Resting the mouse over the instance of a leaked &lt;code&gt;HTMLElement&lt;/code&gt; shows a bit of data about it, it&amp;rsquo;s a &lt;code&gt;aml-rated-album&lt;/code&gt; and it is detached. Definitely a symptom of our leak. Let&amp;rsquo;s see the retainers:&lt;/p&gt;
&lt;p&gt;&lt;img src="images/Scenario2Part4-1.png" alt="Scenario 2 Part 4"&gt;&lt;/p&gt;
&lt;p&gt;Ouch. This is nasty. Again, we are not seeing much that is particularly useful. We have a long graph of retainers starting with the &lt;code&gt;compileNode&lt;/code&gt; function, we also have an array in a &lt;code&gt;n.fn.init&lt;/code&gt; function. To cut a long story short, we&amp;rsquo;re are not going to easily find the root cause here. But I will share some hints.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;jQuery isn&amp;rsquo;t leaking.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;We will end up seeing so much jQuery stuff it is natural to wonder whether jQuery is leaking. Almost certainly not. In the graph about &lt;code&gt;n.fn.init&lt;/code&gt; is just a jQuery selector, held onto by &lt;code&gt;$$element&lt;/code&gt;. No surprise - all angular elements are jQuery or jQuery light objects. We&amp;rsquo;ve leaked an element, it just happens to be wrapped in a jQuery selector. (You might see a different type of graph, probably due to the jQuery 1 + AngularJS 1.2 combination, we&amp;rsquo;ll see it later).&lt;/p&gt;
&lt;p&gt;You may see low level arrays containing data associated with a scope in jQuery, again, don&amp;rsquo;t worry. It&amp;rsquo;s the jQuery data cache (which we&amp;rsquo;ll also see later), which is associating elements to scopes.&lt;/p&gt;
&lt;p&gt;We can try and work through this graph, but let&amp;rsquo;s try another tack.&lt;/p&gt;
&lt;p&gt;It looks like we&amp;rsquo;re probably leaking the whole of the top rated view. We&amp;rsquo;re probably leaking the main scope for the view, created by the &lt;code&gt;TopRatedController&lt;/code&gt;. Let&amp;rsquo;s see if we can find it.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;You can find objects you think are leaking by tagging them with classes!&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This is a neat trick. Let&amp;rsquo;s add a couple of lines to our top rated controller:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-javascript" data-lang="javascript"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;angular&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;module&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;app&amp;#39;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;.&lt;span style="color:#a6e22e"&gt;controller&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;TopRatedController&amp;#39;&lt;/span&gt;, &lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;$scope&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;$http&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;$interval&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// Create a class, assign it to the scope. This&amp;#39;ll help us
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// see if $scope is leaked.
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;function&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;TopRatedControllerTag&lt;/span&gt;() {}
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;$scope&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;__tag&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;new&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;TopRatedControllerTag&lt;/span&gt;();
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// etc...
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;});
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now when we run the analysis again, we can search in the snapshot for &lt;code&gt;TopRatedControllerTag&lt;/code&gt;:&lt;/p&gt;
&lt;p&gt;&lt;img src="images/Scenario2Part5.png" alt="Scenario 2 Part 5"&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;We search for &amp;lsquo;Tag&amp;rsquo;, finding one instance of the &lt;code&gt;TopRatedControllerTag&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Bingo - it is retained by a Scope, with id &lt;code&gt;@534851&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Let&amp;rsquo;s look at this scope in more detail. Right click on it and choose &amp;lsquo;Review in Summary View&amp;rsquo;, so we can see what is retaining it:&lt;/p&gt;
&lt;p&gt;&lt;img src="images/Scenario2Part6.png" alt="Scenario 2 Part 6"&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;We can now see the root scope for the actual view.&lt;/li&gt;
&lt;li&gt;We can see the usual pattern of &lt;code&gt;$$ChildScope&lt;/code&gt; and &lt;code&gt;$parent&lt;/code&gt; properties, but what else have we got?&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Intestingly we can see that our scope is also retained by a &lt;strong&gt;context variable called $scope&lt;/strong&gt;. How do I know it is a context variable? It&amp;rsquo;s in blue, part of the colour coding (see Mystery 3).&lt;/p&gt;
&lt;p&gt;What is a context variable?&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;A &lt;strong&gt;closure&lt;/strong&gt; is a function which refers to a variable outside of its definition. A &lt;strong&gt;context variable&lt;/strong&gt; is the variable stored in a function context. A &lt;strong&gt;function context&lt;/strong&gt; contains the environment for a closure, which is the data required to execute it.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;So basically we have a closure which refers to a variable called &lt;code&gt;$scope&lt;/code&gt;, which is the root scope of our view. We can see in detail the closure:&lt;/p&gt;
&lt;p&gt;&lt;img src="images/Scenario2Part7.png" alt="Scenario 2 Part 7"&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code&gt;$scope&lt;/code&gt; is retained by a &lt;code&gt;context&lt;/code&gt; for a closure.&lt;/li&gt;
&lt;li&gt;The closure is in the &lt;code&gt;refresh&lt;/code&gt; function (this is why the &lt;code&gt;context&lt;/code&gt; is retained by &lt;code&gt;refresh&lt;/code&gt;).&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;We can open the function and examine it for issues. There&amp;rsquo;s an &lt;code&gt;$http.get&lt;/code&gt; which has as closure which uses &lt;code&gt;$scope&lt;/code&gt;, but alarmingly there is an &lt;code&gt;$interval&lt;/code&gt; registered to run every 10 seconds, which is never deregistered. The interval callback uses another &lt;code&gt;$http.get&lt;/code&gt;, with a closure that uses &lt;code&gt;$scope&lt;/code&gt;. This is the problem.&lt;/p&gt;
&lt;p&gt;A simple timeout we forgot to deregister has a closure on &lt;code&gt;$scope&lt;/code&gt;. &lt;code&gt;$scope&lt;/code&gt; can therefore never be cleaned up, because it is retained by a context.&lt;/p&gt;
&lt;p&gt;Some important takeaways:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The framework hides implementation details. Often useful, but in this case it made finding the leak a problem.&lt;/li&gt;
&lt;li&gt;This example seems contrived, but how how often do you have a closure using &lt;code&gt;$scope&lt;/code&gt; in a controller? In real world apps all of the time time, callbacks to ajax requests, event handlers, promise functions etc.&lt;/li&gt;
&lt;li&gt;A leak of a small object that contains the &lt;strong&gt;data&lt;/strong&gt; for three albums has leaked a &lt;strong&gt;large graph&lt;/strong&gt; of other objects, and even &lt;strong&gt;DOM elements&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;blockquote&gt;
&lt;p&gt;Leaks are not incremental. You don&amp;rsquo;t get an accumulation of small leaks, one small leak can retain a huge graph.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Let&amp;rsquo;s talk about this a bit more.&lt;/p&gt;
&lt;h3 id="dealing-with-object-graphs"&gt;Dealing with Object Graphs&lt;/h3&gt;
&lt;p&gt;We saw before that a chain of retainers can pin an object, such as a scope, to a GC root. We also saw that AngularJS scopes are part of a highly connected graph, meaning that if we leak part of it, we probably leak it all:&lt;/p&gt;
&lt;p&gt;&lt;img src="images/ScopeLeakGraph1-1.png" alt="Scope Leak Graph 1"&gt;&lt;/p&gt;
&lt;p&gt;However, things can get worse. Remember how in an angular app you can get the scope for an element with &lt;code&gt;$(selector).scope()&lt;/code&gt;? This connection between a scope an an element is maintained in the jQuery data cache. This lets us associate arbitrary data with an element. This introduces another layer of connectivity:&lt;/p&gt;
&lt;p&gt;&lt;img src="images/ScopeLeakGraph2.png" alt="Scope Leak Graph 2"&gt;&lt;/p&gt;
&lt;p&gt;In this graph, we see the jQuery data cache entries (in grey) associating DOM elements to scopes, introducing more connectivity.&lt;/p&gt;
&lt;p&gt;We can see here an alarming increase in the size and potential complexity of the graph. We&amp;rsquo;ve got DOM elements in play now. The chances are that if you are reading this you are dealing with a memory leak in your app, if it&amp;rsquo;s noticable enough for you to deal with it, you probably have a non-trivial graph.&lt;/p&gt;
&lt;p&gt;So how do we fix memory leaks? I&amp;rsquo;ll show three general approaches and how to use each one.&lt;/p&gt;
&lt;h2 id="fixing-memory-leaks"&gt;Fixing Memory Leaks&lt;/h2&gt;
&lt;p&gt;Fixing memory leaks is hard. As we have seen our problem is highly connected graphs. If we have a part of the graph we want to free for garbage collection (such as a scope and all of it&amp;rsquo;s children, such as a view or directive) then we must not retain that graph of objects. This means if you have (for example) three problems that lead to retaining a graph, you have to fix &lt;strong&gt;all of the problems&lt;/strong&gt; before the leak goes away.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s generalise the best practices first into three rules, see patterns we should follow for each of them and then look at anti-patterns to avoid.&lt;/p&gt;
&lt;h3 id="three-golden-rules"&gt;Three Golden Rules&lt;/h3&gt;
&lt;blockquote&gt;
&lt;p&gt;Rule 1: Understand the framework and lifecycle.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;If you are using a framework like AngularJS, you &lt;strong&gt;must&lt;/strong&gt; understand the lifecycle of the objects you are dealing with. Unless you understand how the framework tries to clean up, you may make mistakes that stop it from working.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Rule 2: Be careful at the interface between short and long lived objects.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Whenever you see an interface between a short and long lived object, be extra careful. For example, if you have a directive talking to a service, make sure the service cannot retain the directive through closures, callbacks or any references. Services will last for the lifetime of the application, so they are the sort of object which can inadvertantly retain short lived objects.&lt;/p&gt;
&lt;p&gt;Other long lived objects exist but may be more subtle, the interface between AngularJS and other libraries can be a risky area, if other libraries maintain long lived state.&lt;/p&gt;
&lt;p&gt;Finally, consider this. The isolated scope for a directive (for example) may inadvertantly be long lived - if it is leaked. That leads us to Rule 3.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Rule 3: Disconnect the graph.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;You can be defensive by manually disconnecting graphs of objects. This can aid if you have a memory leak you cannot resolve. By disconnecting the graph, the garbage collector will at least be able to attempt to clean up parts of it.&lt;/p&gt;
&lt;p&gt;AngularJS should attempt to do this for you, for example when scopes are destroyed the links to other scopes are severed. But you can also do this yourself. Disconnecting the graph is not always as simple as emptying arrays or nulling objects, it can mean nulling closures and context variables too&lt;sup&gt;&lt;a href="#fn5" id="ref5"&gt;5&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;
&lt;p&gt;The anti-patterns which follow are all violations of these rules.&lt;/p&gt;
&lt;h3 id="anti-patterns-to-avoid"&gt;Anti-Patterns to Avoid&lt;/h3&gt;
&lt;p&gt;Whether or not your app is suffering from memory leaks, avoid these patterns.&lt;/p&gt;
&lt;h4 id="poorly-managed-event-handlers"&gt;Poorly Managed Event Handlers&lt;/h4&gt;
&lt;p&gt;Consider a trivial example in a directive link:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-javascript" data-lang="javascript"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;scope&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;element&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;attrs&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;element&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;on&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;click&amp;#39;&lt;/span&gt;, &lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;() {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;scope&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;selected&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;true&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; });
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;We register an event handler. We&amp;rsquo;ve now built a closure which will have a context, which retains the &lt;code&gt;scope&lt;/code&gt;. If we don&amp;rsquo;t deregister this event handler, we retain the closure, the context, the scope, and then basically everything in the universe.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The Fix&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-javascript" data-lang="javascript"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;scope&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;element&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;attrs&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;element&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;on&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;click&amp;#39;&lt;/span&gt;, &lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;() {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;scope&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;selected&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;true&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; });
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;scope&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;$on&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;$destroy&amp;#39;&lt;/span&gt;, &lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;() {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;element&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;off&lt;/span&gt;(); &lt;span style="color:#75715e"&gt;// deregister all event handlers
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; })&lt;span style="color:#e6db74"&gt;&amp;#39;&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;em&gt;Note:&lt;/em&gt; Angular &lt;em&gt;should&lt;/em&gt; handle this. It is supposed to deregister event handlers on elements it manages. In my experience this isn&amp;rsquo;t always the case, although it seems cases when this doesn&amp;rsquo;t happen are fewer and fewer as bugs get fixed in the framework. Anyway, Rule 3 - disconnect.&lt;/p&gt;
&lt;h4 id="poorly-managed-watchers"&gt;Poorly Managed Watchers&lt;/h4&gt;
&lt;p&gt;Watchers or angular event handlers, basically the same as above.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-javascript" data-lang="javascript"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;$scope&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;$on&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;someEvent&amp;#39;&lt;/span&gt;, &lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;() {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;$scope&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;refresh&lt;/span&gt;();
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;})
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Again, Angular should clean this up if you forget to, but the advice is always do it yourself. Angular watchers return a deregister function.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The Fix&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-javascript" data-lang="javascript"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;var&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;cleanup&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;$scope&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;$on&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;someEvent&amp;#39;&lt;/span&gt;, &lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;() {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;$scope&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;refresh&lt;/span&gt;();
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;});
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;$scope&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;$on&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;$destroy&amp;#39;&lt;/span&gt;, &lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;() {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;cleanup&lt;/span&gt;();
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;})
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Rule 1 - know the framework and how lifecycle is handled. &lt;code&gt;$destroy&lt;/code&gt; is sent to a scope specifically to allow it to be cleaned up.&lt;/p&gt;
&lt;h4 id="callback-functions-on-services"&gt;Callback Functions on Services&lt;/h4&gt;
&lt;p&gt;Services (or other long lived objects) should typically not take callback functions. Imagine a &amp;lsquo;user service&amp;rsquo;, allowing a scope to discover if the user has changed their name:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-javascript" data-lang="javascript"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;UserService&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;onNameChange&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;newName&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;$scope&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;userName&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;newName&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;});
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now the service (a long lived object) takes a closure with a context to a short lived object, the scope. Unless the service is written absolutely correctly, we run the risk of the service retaining the scope. Remember, services are singletons and as such the interface between services and scopes is one that requires careful management.&lt;/p&gt;
&lt;p&gt;There are two fixes I would suggest.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Fix 1: For a one-off operation, use a promise&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-javascript" data-lang="javascript"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// change and name and wait for the result
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;UserService&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;changeName&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;Fry&amp;#34;&lt;/span&gt;).&lt;span style="color:#a6e22e"&gt;then&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;newName&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;$scope&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;name&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;newName&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;});
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The notification service returns a promise (a short lived object) which holds the closure. If we get things wrong, we are less likely to leak the scope. Plus, promises are typically easy to work with once you&amp;rsquo;ve got the hang of them&lt;sup&gt;&lt;a href="#fn6" id="ref6"&gt;6&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Fix 2: For notifications, use broadcasts&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-javascript" data-lang="javascript"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// more like our original example
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;$scope&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;$on&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;NotificationService:ChangeName&amp;#39;&lt;/span&gt;, &lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;data&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;$scope&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;name&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;data&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;});
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Some will say to not overuse broadcasts as they can be expensive. They can, so use them judiciously. But remember, they&amp;rsquo;re provided by the framework, typically lead to fairly loose coupling and are probably managing clean up as well or better than a hand-rolled mechanism in a service. Rule 2 - don&amp;rsquo;t tie short lived objects to long lived objects.&lt;/p&gt;
&lt;h2 id="the-future"&gt;The Future&lt;/h2&gt;
&lt;p&gt;That&amp;rsquo;s a wrap. Hopefully this article will grow and improve with feedback from the community. To wind up, lets look at a few things that are on their way which will touch on these issues.&lt;/p&gt;
&lt;h3 id="weak-maps"&gt;Weak Maps&lt;/h3&gt;
&lt;p&gt;Finally, in ECMAScript 6 we will get a WeakMap&lt;sup&gt;&lt;a href="#fn7" id="ref7"&gt;7&lt;/a&gt;&lt;/sup&gt; object. This is &lt;em&gt;ideal&lt;/em&gt; for something like the jQuery data cache. A weak map uses weak references (not natively supported in JavaScript). This means that we can map a DOM element to a scope in a weak map, but the map entry doesn&amp;rsquo;t retain the element or scope. If the element or scope is cleaned up, the map entry is removed. This means internal structures to aid with frameworks don&amp;rsquo;t need to necessarily retain object graphs.&lt;/p&gt;
&lt;h3 id="angularjs-2"&gt;AngularJS 2&lt;/h3&gt;
&lt;p&gt;Simplifications to the framework in 2.0 and usage of native features like web components mean less complex framework code and less scope for issues. Consider even the usage of classes in Angular 2.0. We don&amp;rsquo;t decorate a scope object (of type &lt;code&gt;Object&lt;/code&gt;) we create an instance of a class. Easier to see in the heap view.&lt;/p&gt;
&lt;h3 id="even-better-browsers"&gt;Even Better Browsers&lt;/h3&gt;
&lt;p&gt;SPA frameworks are driving improvements to browsers. Frameworks like Angular lead to more SPAs. More SPAs mean we find more bugs and edge cases in browsers. Many memory leak issues in AngularJS have led to fixes in V8.&lt;/p&gt;
&lt;h2 id="appendices"&gt;Appendices&lt;/h2&gt;
&lt;p&gt;Beware any write up long enough to need appendices.&lt;/p&gt;
&lt;h3 id="thanks"&gt;Thanks&lt;/h3&gt;
&lt;p&gt;Much of my understanding here came from working with others on real-world issues. I would like to thank the following people for their advice and insights:&lt;/p&gt;
&lt;p&gt;James Denning, Shaun Bohannon, Arnaud Rebts, Colin Montgomery, Jon Hamshaw, Christian Lilley, Maarten De Wilde&lt;/p&gt;
&lt;p&gt;There are others I have worked on with in this area, if I have forgotten to mention you please let me know.&lt;/p&gt;
&lt;h3 id="mysteries"&gt;Mysteries&lt;/h3&gt;
&lt;p&gt;After a large amount of time spent investigating memory leaks, there are still some things which to me are a mystery. If anyone can shed some light, I&amp;rsquo;d be interested to know.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Mystery 1: False Charts&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;As mentioned earlier we cannot always trust the timeline, it is not uncommon to see the memory usage in the timeline increase, even though the size of snapshots seems to be staying constant. This may be related to AngularJS Issue &lt;a href="https://github.com/angular/angular.js/issues/4864"&gt;DOM Nodes Leaking&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Mystery 2: Odd Snapshot Sizes&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;It is not uncommon for the first snapshot to be large, and then subsequent snapshots to all be a bit smaller (even without any state changes). Why this is the case I do not know. To test, run an angular app and take some snapshots without doing anything in between. You&amp;rsquo;ll normally see (for example) 9 MB, 9MB, 9MB. However, it is not uncommon to see 15 MB, 9MB, 9MB.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Mystery 3: Where&amp;rsquo;s the colour coding documentation?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The Chrome documentation states that the colour coding key for elements in the heap snapshot is available in the tool. I can&amp;rsquo;t find it anywhere, so had to research to find the details.&lt;/p&gt;
&lt;h3 id="further-reading"&gt;Further Reading&lt;/h3&gt;
&lt;p&gt;Still not had enough? Try these.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href="https://developer.chrome.com/devtools/docs/heap-profiling#basics"&gt;Profiling Memory Performance&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.chrome.com/devtools/docs/memory-analysis-101"&gt;Memory Analysis 101&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.chrome.com/devtools/docs/heap-profiling-containment"&gt;Heap profile containment&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.chrome.com/devtools/docs/tips-and-tricks"&gt;Dev tools tips &amp;amp; tricks&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.chrome.com/devtools/docs/javascript-memory-profiling"&gt;JavaScript Memory Profiling&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_Management"&gt;Memory Management&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://addyosmani.com/blog/taming-the-unicorn-easing-javascript-memory-profiling-in-devtools/"&gt;Taming the Unicorn&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;hr&gt;
&lt;p&gt;&lt;sup id="fn1"&gt;1. This is C actually, but the syntax isn&amp;rsquo;t important, just the logic of what we&amp;rsquo;re doing.&lt;a href="#ref1"&gt;↩&lt;/a&gt;&lt;/sup&gt;
&lt;sup id="fn2"&gt;2. In JavaScript as in most managed languages, the mechanism by which this happens is reference counting and garbage collection. There&amp;rsquo;s a superb description at &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_Management"&gt;JavaScript Memory Management&lt;/a&gt;.&lt;a href="#ref2"&gt;↩&lt;/a&gt;&lt;/sup&gt;
&lt;sup id="fn3"&gt;3. Try it yourself with this &lt;a href="http://jsfiddle.net/dwmkerr/2LzxgLb4/"&gt;fiddle for a sawtooth pattern&lt;/a&gt;.&lt;a href="#ref3"&gt;↩&lt;/a&gt;&lt;/sup&gt;
&lt;sup id="fn4"&gt;4. Try it yourself with this &lt;a href="http://jsfiddle.net/dwmkerr/9dmpp5te/"&gt;fiddle for a &amp;lsquo;steps&amp;rsquo; pattern&lt;/a&gt;.&lt;a href="#ref4"&gt;↩&lt;/a&gt;&lt;/sup&gt;
&lt;sup id="fn5"&gt;5. See &lt;a href="https://github.com/dwmkerr/angular-modal-service/commit/79998ca98101798608bdb914aecbd44f3ccbaa7a"&gt;this commit&lt;/a&gt; in my Angular Modal Service for an example of how nulling context variables (i.e. disconnecting the graph) solved a memory leak. This is a good example of how are it can be, after large amounts of analysis I still haven&amp;rsquo;t discovered &lt;strong&gt;why&lt;/strong&gt; this was needed, but it solved the problem. It may relate to Mystery 4.&lt;a href="#ref5"&gt;↩&lt;/a&gt;&lt;/sup&gt;
&lt;sup id="fn6"&gt;6. See my article &lt;a href="http://www.dwmkerr.com/promises-in-angularjs-the-definitive-guide/"&gt;Promises in AngularJS - The Definitive Guide&lt;/a&gt; if you are not sure how to use them.&lt;a href="#ref6"&gt;↩&lt;/a&gt;&lt;/sup&gt;
&lt;sup id="fn7"&gt;7. More details at &lt;a href="https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/WeakMap"&gt;https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/WeakMap&lt;/a&gt;&lt;a href="#ref7"&gt;↩&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;</description><category>CodeProject</category></item><item><title>The Only AngularJS Modal Service You'll Ever Need</title><link>https://dwmkerr.com/the-only-angularjs-modal-service-youll-ever-need/</link><pubDate>Mon, 16 Jun 2014 00:48:12 +0000</pubDate><guid>https://dwmkerr.com/the-only-angularjs-modal-service-youll-ever-need/</guid><description>&lt;p&gt;If you need modals in an AngularJS application, look no further. I&amp;rsquo;ll show you how to use the &lt;a href="https://github.com/dwmkerr/angular-modal-service"&gt;Angular Modal Service&lt;/a&gt; to add Bootstrap Modals or your own custom modals to your application.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://jsfiddle.net/dwmkerr/8MVLJ/"&gt;See it in a fiddle&lt;/a&gt; or check out &lt;a href="http://dwmkerr.github.io/angular-modal-service"&gt;a full set of samples online&lt;/a&gt;.&lt;/p&gt;
&lt;h4 id="contents"&gt;Contents&lt;/h4&gt;
&lt;ol&gt;
&lt;li&gt;[Using the Angular Modal Service](#UsingTheAngular ModalService)&lt;/li&gt;
&lt;li&gt;&lt;a href="#AQuickExample"&gt;A Quick Example&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#DesignGoals"&gt;Design Goals&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#HowItWorks"&gt;How It Works&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#WrappingUp"&gt;Wrapping Up&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id="using-the-angular-modal-service"&gt;Using the Angular Modal Service&lt;/h2&gt;
&lt;p&gt;Here&amp;rsquo;s how you can use the Angular Modal Service to add a bootstrap modal to your application.&lt;/p&gt;
&lt;h4 id="step-1-install-with-bower"&gt;Step 1: Install with Bower&lt;/h4&gt;
&lt;p&gt;Install the service with bower:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;bower install angular-modal-service --save
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;If you don&amp;rsquo;t use bower, just get the source directly from the &lt;a href="https://github.com/dwmkerr/angular-modal-service/tree/master/dst"&gt;&lt;code&gt;dst&lt;/code&gt;&lt;/a&gt; folder of the repo.&lt;/p&gt;
&lt;h4 id="step-2-include-the-javascript"&gt;Step 2: Include the JavaScript&lt;/h4&gt;
&lt;p&gt;Include the JavaScript from the &lt;code&gt;dst&lt;/code&gt; folder or require it with require.js:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-html" data-lang="html"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&amp;lt;&lt;span style="color:#f92672"&gt;script&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;src&lt;/span&gt;&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#34;bower_components\angular-modal-service\dst\angular-modal-service.min.js&amp;#34;&lt;/span&gt;&amp;gt;&amp;lt;/&lt;span style="color:#f92672"&gt;script&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h4 id="step-3-add-it-as-a-dependency"&gt;Step 3: Add it as a dependency&lt;/h4&gt;
&lt;p&gt;Make sure the &lt;code&gt;angularModalService&lt;/code&gt; module is listed as a required module for your application:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;var&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;app&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;angular&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;module&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;myApp&amp;#39;&lt;/span&gt;, [&lt;span style="color:#e6db74"&gt;&amp;#39;angularModalService&amp;#39;&lt;/span&gt;]);
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h4 id="step-4-show-the-modal"&gt;Step 4: Show the Modal&lt;/h4&gt;
&lt;p&gt;Inject &lt;code&gt;ModalService&lt;/code&gt; into any controller, directive or service and call the &lt;code&gt;showModal&lt;/code&gt; function to show a modal:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code class="language-language" data-lang="language"&gt;app.controller(&amp;#39;SampleController&amp;#39;, function($scope, ModalService) {
ModalService.showModal({
templateUrl: &amp;#34;template.html&amp;#34;,
controller: &amp;#34;ModalController&amp;#34;
}).then(function(modal) {
//it&amp;#39;s a bootstrap element, use &amp;#39;modal&amp;#39; to show it
modal.element.modal();
modal.close.then(function(result) {
console.log(result);
});
});
);
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This code loads the HTML from &lt;code&gt;template.html&lt;/code&gt;, adds it to the DOM, creates a scope for it and creates an instance of a &lt;code&gt;ModalController&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;When this is done, the promise returned by the &lt;code&gt;showModal&lt;/code&gt; function resolves and you get a &lt;code&gt;modal&lt;/code&gt; object. This object contains the element created. If it&amp;rsquo;s a Bootstrap modal just call &lt;code&gt;modal&lt;/code&gt; to show it, if it&amp;rsquo;s a custom one you can show it by changing its CSS styles or using whatever APIs are provided. There&amp;rsquo;s an example ofa custom modal in &lt;a href="http://dwmkerr.github.io/angular-modal-service/"&gt;the samples&lt;/a&gt;.&lt;/p&gt;
&lt;h4 id="step-5-close-the-modal"&gt;Step 5: Close the Modal&lt;/h4&gt;
&lt;p&gt;The controller that is created always has one extra parameter injected into it - a function called &lt;code&gt;close&lt;/code&gt;. Call this function to close the modal, anything you pass to it is passed to the caller as the &lt;code&gt;result&lt;/code&gt; object.&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code class="language-language" data-lang="language"&gt;app.controller(&amp;#39;ModalController&amp;#39;, function($scope, close) {
// when you need to close the modal, call close
close(&amp;#34;Success!&amp;#34;);
});
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;You can pass a number of milliseconds to wait before destroying the DOM element as an optional second parameter to &lt;code&gt;close&lt;/code&gt; - this is useful if the closing of the modal is animated and you don&amp;rsquo;t want it to disappear before the animation completes.&lt;/p&gt;
&lt;h2 id="a-quick-example"&gt;A Quick Example&lt;/h2&gt;
&lt;p&gt;Here&amp;rsquo;s a fiddle of the modal service in action:&lt;/p&gt;
&lt;iframe width="100%" height="300" src="http://jsfiddle.net/dwmkerr/8MVLJ/embedded/result,js,html" allowfullscreen="allowfullscreen" frameborder="0"&gt;&lt;/iframe&gt;
&lt;p&gt;One thing to note in this examples is that the template is just declared in the DOM - this works fine because the service always checks the template cache before attempting to load it from the server.&lt;/p&gt;
&lt;p&gt;There are more examples at &lt;a href="http://dwmkerr.github.io/angular-modal-service/"&gt;dwmkerr.github.io/angular-modal-service&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id="design-goals"&gt;Design Goals&lt;/h2&gt;
&lt;p&gt;There are some other services for handling modals out there, notably &lt;a href="https://github.com/Fundoo-Solutions/angularjs-modal-service"&gt;Fundoo&amp;rsquo;s Modal Service&lt;/a&gt; and a few others. However, the design goals for my service were slightly different:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;No link to bootstrap&lt;/strong&gt;. Bootstrap modals are complex with lots of options - if you want to use them then that&amp;rsquo;s great, the service should work with them, but the complexity of the options for Bootstrap Modals should not increase the complexity of the service.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Extremely simple code&lt;/strong&gt;. It&amp;rsquo;s rare you&amp;rsquo;ll write something that it will suit everyone&amp;rsquo;s need. Rather than trying to please everyone, I want a service that is simple enough to understand so that it can be easily adapted by others.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;So the core goal here is simplicity - if others can understand the code, then they can more effectively decide whether it&amp;rsquo;s what they need, or build upon it.&lt;/p&gt;
&lt;p&gt;With these design goals in mind I built the angular modal service.&lt;/p&gt;
&lt;h2 id="how-it-works"&gt;How It Works&lt;/h2&gt;
&lt;p&gt;I&amp;rsquo;m going to walk through a slightly simplified version of the code because it actually illustrates quite a few important concepts when working with AngularJS.&lt;/p&gt;
&lt;p&gt;One of the things that&amp;rsquo;s useful to know is that this service creates a DOM element, builds a scope for it and instantiates a controller for it - what we&amp;rsquo;re doing is &lt;em&gt;very&lt;/em&gt; similar to what AngularJS does behind the scenes when a directive is created.&lt;/p&gt;
&lt;p&gt;So let&amp;rsquo;s dive in. We&amp;rsquo;re going to define a service, so we need a module.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;var&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;module&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;angular&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;module&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;angularModalService&amp;#39;&lt;/span&gt;, []);
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now we have our module, we can define our service. I tend to write services in the form of classes, but this is a personal choice - it&amp;rsquo;s just as valid to return a javascript object that contains functions and data.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;module&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;factory&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;ModalService&amp;#39;&lt;/span&gt;, [&lt;span style="color:#e6db74"&gt;&amp;#39;$document&amp;#39;&lt;/span&gt;, &lt;span style="color:#e6db74"&gt;&amp;#39;$compile&amp;#39;&lt;/span&gt;, &lt;span style="color:#e6db74"&gt;&amp;#39;$controller&amp;#39;&lt;/span&gt;, &lt;span style="color:#e6db74"&gt;&amp;#39;$http&amp;#39;&lt;/span&gt;, &lt;span style="color:#e6db74"&gt;&amp;#39;$rootScope&amp;#39;&lt;/span&gt;, &lt;span style="color:#e6db74"&gt;&amp;#39;$q&amp;#39;&lt;/span&gt;, &lt;span style="color:#e6db74"&gt;&amp;#39;$timeout&amp;#39;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;$document&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;$compile&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;$controller&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;$http&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;$rootScope&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;$q&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;$timeout&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;I need a lot of injected components, we&amp;rsquo;ll see why as we continue. I also use the explicit form of the function which takes the parameters as strings - this is the only safe way to write an injected function if you are minifying code.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;var&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;body&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;$document&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;find&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;body&amp;#39;&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;function&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;ModalService&lt;/span&gt;() {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;var&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;self&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;this&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;I use the &lt;code&gt;$document&lt;/code&gt; object to get the body element, which the modal will be appended to. I then create a class function and record &lt;code&gt;this&lt;/code&gt; as self, so that I can refer to the class instance in callbacks and so on.&lt;/p&gt;
&lt;p&gt;The next part of the code creates a function that will return the template, given either a raw template string or a template url. The reason we wrap this function like this is that the operation will either be synchronous or asynchronous, and I don&amp;rsquo;t want the caller to care. So we use promises to wrap the logic.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;var&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;getTemplate&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;template&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;templateUrl&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;var&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;deferred&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;$q&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;defer&lt;/span&gt;();
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;if&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;template&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;deferred&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;resolve&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;template&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; } &lt;span style="color:#66d9ef"&gt;else&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;if&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;templateUrl&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;$http&lt;/span&gt;({&lt;span style="color:#a6e22e"&gt;method&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;GET&amp;#39;&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;url&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;templateUrl&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;cache&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;true&lt;/span&gt;})
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; .&lt;span style="color:#a6e22e"&gt;then&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;result&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;deferred&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;resolve&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;result&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;data&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; })
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; .&lt;span style="color:#66d9ef"&gt;catch&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;error&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;deferred&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;reject&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;error&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; });
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; } &lt;span style="color:#66d9ef"&gt;else&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;deferred&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;reject&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;No template or templateUrl has been specified.&amp;#34;&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;deferred&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;promise&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;};
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;If any of this seems confusing, check out my article &lt;a href="http://www.dwmkerr.com/promises-in-angularjs-the-definitive-guide/"&gt;AngularJS Promises - The Definitive Guide&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Now to the main function.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;self&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;showModal&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;options&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;var&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;deferred&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;$q&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;defer&lt;/span&gt;();
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The &lt;code&gt;showModal&lt;/code&gt; function is going to have to do all sorts of async work - loading the template from the server and so on. So we are going to create a &lt;code&gt;deferred&lt;/code&gt; object and build a promise to return to the caller.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;var&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;controller&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;options&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;controller&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;if&lt;/span&gt;(&lt;span style="color:#f92672"&gt;!&lt;/span&gt;&lt;span style="color:#a6e22e"&gt;controller&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;deferred&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;reject&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;No controller has been specified.&amp;#34;&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;deferred&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;promise&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now we validate that a controller has been passed in as part of the options. Notice how just like in &lt;code&gt;getTemplate&lt;/code&gt; we use the &lt;code&gt;reject&lt;/code&gt; function to deal with error cases. Again, if error handling with promises seems unfamiliar, check out &lt;a href="http://www.dwmkerr.com/promises-in-angularjs-the-definitive-guide/"&gt;AngularJS Promises - The Definitive Guide&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Next we deal with the template.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;getTemplate&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;options&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;template&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;options&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;templateUrl&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; .&lt;span style="color:#a6e22e"&gt;then&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;template&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;We&amp;rsquo;ve used the &lt;code&gt;getTemplate&lt;/code&gt; function to get the template, sync or async it doesn&amp;rsquo;t matter, our logic is the same.&lt;/p&gt;
&lt;p&gt;Now we can build a new scope for our modal.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;var&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;modalScope&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;$rootScope&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;$new&lt;/span&gt;();
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;We&amp;rsquo;ll refer to this a lot later on. Now for some cleverness.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;var&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;closeDeferred&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;$q&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;defer&lt;/span&gt;();
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;var&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;inputs&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;$scope&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;modalScope&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;close&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;result&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;delay&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;if&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;delay&lt;/span&gt; &lt;span style="color:#f92672"&gt;===&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;undefined&lt;/span&gt; &lt;span style="color:#f92672"&gt;||&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;delay&lt;/span&gt; &lt;span style="color:#f92672"&gt;===&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;null&lt;/span&gt;) &lt;span style="color:#a6e22e"&gt;delay&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;$timeout&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;function&lt;/span&gt; () {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;closeDeferred&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;resolve&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;result&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }, &lt;span style="color:#a6e22e"&gt;delay&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;};
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This requires some explanation. First, we create a new &lt;code&gt;deferred&lt;/code&gt; object. This is going to be used to build a promise that is resolved when the modal closes.&lt;/p&gt;
&lt;p&gt;Now we build an &lt;code&gt;input&lt;/code&gt; object. This contains parameters we want to inject to the controller we&amp;rsquo;re going to create. Any parameters the controller needs, such as &lt;code&gt;$element&lt;/code&gt;, &lt;code&gt;$timeout&lt;/code&gt; or whatever will be injected by angular. We&amp;rsquo;re just going to make sure that the &lt;code&gt;$scope&lt;/code&gt; that is injected is the one we&amp;rsquo;ve just created, and that we also inject a function called &amp;lsquo;close&amp;rsquo;. This function simply resolves the promise we&amp;rsquo;ve created after a specified timeout.&lt;/p&gt;
&lt;p&gt;This means that any controller for a modal can take &lt;code&gt;close&lt;/code&gt; as a parameter, and we&amp;rsquo;ll inject the function that resolves the promise. This promise is returned to the consumer so that they can take action when the modal closes. We also allow the controller to pass a variable to &lt;code&gt;close&lt;/code&gt; which is passed to the &lt;code&gt;resolve&lt;/code&gt; function as well.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;if&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;options&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;inputs&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;for&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;var&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;inputName&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;in&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;options&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;inputs&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;inputs&lt;/span&gt;[&lt;span style="color:#a6e22e"&gt;inputName&lt;/span&gt;] &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;options&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;inputs&lt;/span&gt;[&lt;span style="color:#a6e22e"&gt;inputName&lt;/span&gt;];
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Without the this code, the service is close to useless. What we do here is allow the caller to provide extra inputs to the controller. Imagine we have a list of items, maybe books for a library program, and when the use clicks on one we want to show a modal. The code that shows the modal needs to pass the selected book to the modal controller - by adding it to the &lt;code&gt;inputs&lt;/code&gt; object, the book can be injected into the controller. This allows to client to pass data &lt;strong&gt;to&lt;/strong&gt; the controller, with the parameter of the &lt;code&gt;close&lt;/code&gt; function used to return data &lt;strong&gt;from&lt;/strong&gt; the controller.&lt;/p&gt;
&lt;p&gt;Ready for some lower level Angular?&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;var&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;modalController&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;$controller&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;controller&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;inputs&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;var&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;modalElementTemplate&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;angular&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;element&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;template&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;var&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;linkFn&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;$compile&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;modalElementTemplate&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;var&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;modalElement&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;linkFn&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;modalScope&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Four innocuous lines that are actually quite complex.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;First, we create an instance of the controller with name &lt;code&gt;controller&lt;/code&gt;. Regardless of what AngularJS injects, we provide &lt;code&gt;inputs&lt;/code&gt; to be injected as well.&lt;/li&gt;
&lt;li&gt;Now we turn our raw template html into an AngularJS DOM element. AngularJS always works with jQuery or jQuery Lite elements, the &lt;code&gt;angular.element&lt;/code&gt; function takes raw HTML and turns it into a DOM element we can work with.&lt;/li&gt;
&lt;li&gt;Now we &lt;code&gt;$compile&lt;/code&gt; the element. This step goes over the DOM and expands all directives. We&amp;rsquo;re turning raw DOM elements into elements that are expanded into directives, but we haven&amp;rsquo;t yet linked this set of elements into a scope. This is the first step of the compile/link process.&lt;/li&gt;
&lt;li&gt;Finally, we can link the element. The &lt;code&gt;$compile&lt;/code&gt; function returns a link function which we call with a scope to link the DOM elements (fully expanded) to the specified scope.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;This is very similar to AngularJS actually handles directives itself - creating a scope, loading a template, turning it into an element, compiling it and linking it.&lt;/p&gt;
&lt;p&gt;Why are compile and link separate steps? Think of it like this, the work that is done in compile is actually identical for each instance of a directive (or modal in our case). It&amp;rsquo;s not related to an &lt;em&gt;instance&lt;/em&gt; of a directive or modal, it&amp;rsquo;s just expanding the elements and directives. So this work can be done once only, saving a lot of time - then we just call link to create an &lt;em&gt;instance&lt;/em&gt; of our element, bound to a specific scope. So link logic is always per instance (you have a scope, you can &lt;code&gt;$watch&lt;/code&gt; and so on) whereas compile logic is per &lt;em&gt;type&lt;/em&gt; of directive.&lt;/p&gt;
&lt;p&gt;Based on this, we could in fact cache the results of the compile function on a per-template basis, as they can be reused and linked to a scope as necessary. However this is an optimisation that is currently left out.&lt;/p&gt;
&lt;p&gt;Now we can add the fully built element to the DOM and build our return object.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;body&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;append&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;modalElement&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;var&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;modal&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;controller&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;modalController&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;scope&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;modalScope&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;element&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;modalElement&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;close&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;closeDeferred&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;promise&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;};
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;We return the four things the caller might need - the controller, scope, element and close promise. When the close promise is resolved, we also want to clean up:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;modal&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;close&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;then&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;result&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;modalScope&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;$destroy&lt;/span&gt;();
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;modalElement&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;remove&lt;/span&gt;();
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;});
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;deferred&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;resolve&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;modal&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;So when &lt;code&gt;close&lt;/code&gt; is resolved, whatever happens we&amp;rsquo;ll destroy the scope and clean up the DOM. Now we can resolve our promise with the &lt;code&gt;modal&lt;/code&gt; object we&amp;rsquo;ve built&amp;hellip;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; .&lt;span style="color:#66d9ef"&gt;catch&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;error&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;deferred&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;reject&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;error&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;});
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;deferred&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;promise&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&amp;hellip;and we can pass errors that occured during &lt;code&gt;getTemplate&lt;/code&gt; to the caller and finally return the promise we&amp;rsquo;ve built.&lt;/p&gt;
&lt;p&gt;That&amp;rsquo;s it! With this design we handle errors correctly, can pass data to and from the modal, clean up after ourselves and make sure that units of asynchronous work are handled with the standard pattern of promises.&lt;/p&gt;
&lt;h2 id="wrapping-up"&gt;Wrapping Up&lt;/h2&gt;
&lt;p&gt;I hope you&amp;rsquo;ve found the service and some of the details of the code useful, as always comments are welcome, fork the code and have a play - let me know if you think of improvements or have questions,&lt;/p&gt;</description><category>CodeProject</category></item><item><title>AngularJS Promises - The Definitive Guide</title><link>https://dwmkerr.com/promises-in-angularjs-the-definitive-guide/</link><pubDate>Wed, 07 May 2014 12:06:55 +0000</pubDate><guid>https://dwmkerr.com/promises-in-angularjs-the-definitive-guide/</guid><description>&lt;p&gt;Promises are a core feature of AngularJS - whether you understand them or not, if you use AngularJS you&amp;rsquo;ve almost certainly been using them for a while.&lt;/p&gt;
&lt;p&gt;In this post I&amp;rsquo;m going to explain what promises are, how they work, where they&amp;rsquo;re used and finally how to use them effectively.&lt;/p&gt;
&lt;p&gt;Once we&amp;rsquo;ve got the core understanding of promises, we&amp;rsquo;ll look at some more advanced functionality - chaining and resolving promises when routing.&lt;/p&gt;
&lt;h4 id="contents"&gt;Contents&lt;/h4&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href="#whatarepromises"&gt;What are Promises?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#howdopromiseswork"&gt;How do Promises Work?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#arealworldexample"&gt;A Real World Example&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#promisessuccesserrorthen"&gt;Promises - Success, Error, Then&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#advancedpromiseschaining"&gt;Advanced Promises - Chaining&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#advancedpromisesrouting"&gt;Advanced Promises - Routing&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#advancedpromisestipstricks"&gt;Advanced Promises - Tips &amp;amp; Tricks&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#thefutureofpromises"&gt;The Future of Promises&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#wrappingup"&gt;Wrapping Up&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id="what-are-promises"&gt;What are Promises?&lt;/h2&gt;
&lt;p&gt;I&amp;rsquo;m going to try and be as succinct as possible - if anyone has a shorter, clearer description, let me know!&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;A promise represents the eventual result of an operation. You can use a promise to specify what to do when an operation eventually succeeds or fails.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;So let&amp;rsquo;s see this in action. Look at the code below:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;$http&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;get&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;/api/my/name&amp;#34;&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This code uses the &lt;code&gt;$http&lt;/code&gt; service to perform an HTTP GET on the url &amp;lsquo;/api/my/name&amp;rsquo;. Let&amp;rsquo;s say that this is an api we&amp;rsquo;ve implemented on our server that returns the name of the logged in user.&lt;/p&gt;
&lt;p&gt;Now a common mistake for JavaScript newcomers might be to assume that the function returns the name:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// The WRONG way!
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;var&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;$http&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;get&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;/api/my/name&amp;#34;&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;It doesn&amp;rsquo;t - and in fact it can&amp;rsquo;t. An HTTP request has to be executed, it&amp;rsquo;ll take a while before it returns - it might not return at all if there are errors. Remember, when we make requests in JavaScript we&amp;rsquo;re using &lt;strong&gt;ajax&lt;/strong&gt; which is &lt;em&gt;&lt;strong&gt;asynchronous&lt;/strong&gt; javascript and xml&lt;/em&gt;. The key word here is asynchronous - we return control to the browser, let it make a request and give it a function to call when the request completes.&lt;/p&gt;
&lt;p&gt;So let&amp;rsquo;s see how you actually make the request.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;var&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;promise&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;$http&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;get&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;/api/my/name&amp;#34;&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;promise&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;success&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;console&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;log&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;Your name is: &amp;#34;&lt;/span&gt; &lt;span style="color:#f92672"&gt;+&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;});
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;promise&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;error&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;response&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;status&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;console&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;log&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;The request failed with response &amp;#34;&lt;/span&gt; &lt;span style="color:#f92672"&gt;+&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;response&lt;/span&gt; &lt;span style="color:#f92672"&gt;+&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34; and status code &amp;#34;&lt;/span&gt; &lt;span style="color:#f92672"&gt;+&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;status&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;});
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now we use the promise object to specify what to do when the request succeeds, or when it fails. Remember, the functions we pass to &lt;code&gt;success&lt;/code&gt; or &lt;code&gt;error&lt;/code&gt; will be called later - when this block is finished executing we don&amp;rsquo;t have the name, we&amp;rsquo;ve just specified what to do when we &lt;em&gt;do&lt;/em&gt; eventually get it - or what to do if we fail to get it.&lt;/p&gt;
&lt;p&gt;As a convenience, the &lt;code&gt;success&lt;/code&gt; and &lt;code&gt;error&lt;/code&gt; functions actually just return the promise, so we can simplify the code:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;$http&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;get&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;/api/my/name&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; .&lt;span style="color:#a6e22e"&gt;success&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;console&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;log&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;Your name is: &amp;#34;&lt;/span&gt; &lt;span style="color:#f92672"&gt;+&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; })
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; .&lt;span style="color:#a6e22e"&gt;error&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;response&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;status&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;console&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;log&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;The request failed with response &amp;#34;&lt;/span&gt; &lt;span style="color:#f92672"&gt;+&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;response&lt;/span&gt; &lt;span style="color:#f92672"&gt;+&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34; and status code &amp;#34;&lt;/span&gt; &lt;span style="color:#f92672"&gt;+&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;status&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; });
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In fact, &lt;code&gt;success&lt;/code&gt; and &lt;code&gt;error&lt;/code&gt; are special functions added to a promise by &lt;code&gt;$http&lt;/code&gt; - normally with promises we just use &lt;code&gt;then&lt;/code&gt;, which takes the success function as the first parameter and the error function as the second:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;$http&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;get&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;/api/my/name&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; .&lt;span style="color:#a6e22e"&gt;then&lt;/span&gt;(
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;/* success */&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;response&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;console&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;log&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;Your name is: &amp;#34;&lt;/span&gt; &lt;span style="color:#f92672"&gt;+&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;response&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;data&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; },
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;/* failure */&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;error&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;console&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;log&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;The request failed: &amp;#34;&lt;/span&gt; &lt;span style="color:#f92672"&gt;+&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;error&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; });
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;We&amp;rsquo;ll see more about the difference between &lt;code&gt;success&lt;/code&gt;, &lt;code&gt;error&lt;/code&gt; and &lt;code&gt;then&lt;/code&gt; later.&lt;/p&gt;
&lt;p&gt;That&amp;rsquo;s all there is to it - a promise lets us specify what to do as the result of an operation.&lt;/p&gt;
&lt;h2 id="how-do-promises-work"&gt;How do Promises Work?&lt;/h2&gt;
&lt;p&gt;Promises are not actually complicated, they&amp;rsquo;re objects that contain a reference to functions to call when something fails or succeeds.&lt;/p&gt;
&lt;p&gt;Under the hood, AngularJS actually wires up a promise for an HTTP request in a way a bit like this:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;var&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;request&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;new&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;XMLHttpRequest&lt;/span&gt;();
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;request&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;addEventListener&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;load&amp;#34;&lt;/span&gt;, &lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;() {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// complete the promise
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}, &lt;span style="color:#66d9ef"&gt;false&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;request&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;addEventListener&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;error&amp;#34;&lt;/span&gt;, &lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;() {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// fail the promise
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}, &lt;span style="color:#66d9ef"&gt;false&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;request&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;open&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;GET&amp;#34;&lt;/span&gt;, &lt;span style="color:#e6db74"&gt;&amp;#34;/api/my/name&amp;#34;&lt;/span&gt;, &lt;span style="color:#66d9ef"&gt;true&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;request&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;send&lt;/span&gt;();
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;this is pseudo-code, but the idea is that its the browser that calls us back, via the event listeners, then AngularJS can just call the appropriate method on the promise.&lt;/p&gt;
&lt;p&gt;Now in AngularJS, the promises are created with the &lt;code&gt;$q&lt;/code&gt; service (we&amp;rsquo;ll see exactly how to do this shortly), but why &lt;code&gt;$q&lt;/code&gt;?&lt;/p&gt;
&lt;p&gt;The reason the service is named &lt;code&gt;$q&lt;/code&gt; is that AngularJS&amp;rsquo; promise implementation is based on Kris Kowal&amp;rsquo;s promise mechanism, which is called &amp;lsquo;Q&amp;rsquo;. You can see the library at &lt;a href="https://github.com/kriskowal/q"&gt;github.com/kristkowal/q&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;This was a deliberate decision, as the Q library is widely used and well understood by the community. We&amp;rsquo;re going to see a little bit later what the future of promises is in AngularJS and actually in ECMAScript 6.&lt;/p&gt;
&lt;h3 id="a-real-world-example"&gt;A Real World Example&lt;/h3&gt;
&lt;p&gt;In this example we&amp;rsquo;ll create a service that gets the user&amp;rsquo;s name, just like in our examples. However, to make it interesting, we&amp;rsquo;ll set our service up so that the first time we get the name from the server, and then afterwards we&amp;rsquo;ll return a cached copy.&lt;/p&gt;
&lt;p&gt;This means we&amp;rsquo;ll have to build our code to deal with the asynchronous case (the first one) and the more trivial synchronous case (getting the name from the cache).&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s look at a pure asynchronous implementation.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;app&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;factory&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;NameService&amp;#39;&lt;/span&gt;, &lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;$http&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;$q&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// Create a class that represents our name service.
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;function&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;NameService&lt;/span&gt;() {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;var&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;self&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;this&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// getName returns a promise which when
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// fulfilled returns the name.
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;self&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;getName&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;() {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;$http&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;get&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;/api/my/name&amp;#39;&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; };
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;new&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;NameService&lt;/span&gt;();
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;});
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Here&amp;rsquo;s how it looks in a fiddle - just click &amp;lsquo;Result&amp;rsquo; to see it working. You can click on &amp;lsquo;Update&amp;rsquo; name to get the name, but each time it sends a request. This is what we&amp;rsquo;ll change next.&lt;/p&gt;
&lt;iframe width="100%" height="300" src="http://jsfiddle.net/dwmkerr/4GjtR/embedded/js,html,result" allowfullscreen="allowfullscreen" frameborder="0"&gt;&lt;/iframe&gt;
&lt;p&gt;Now let&amp;rsquo;s update our service so that we hit the server only if we haven&amp;rsquo;t already cached the name. I&amp;rsquo;ll build the service blow by blow, then we can see a fiddle of it working.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;app&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;factory&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;NameService&amp;#39;&lt;/span&gt;, &lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;$http&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;$q&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// Create a class that represents our name service.
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;function&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;NameService&lt;/span&gt;() {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;var&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;self&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;this&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// Initially the name is unknown....
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;self&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;name&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;null&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;so first we create a service which is in the form of a class. It has a name field which is initially null.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;self&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;getName&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;() {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// Create a deferred operation.
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;var&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;deferred&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;$q&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;defer&lt;/span&gt;();
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now in the &lt;code&gt;getName&lt;/code&gt; function we start by creating a &lt;code&gt;deferred&lt;/code&gt; object, using the &lt;code&gt;$q&lt;/code&gt; service. This object contains the promise we&amp;rsquo;ll return, and has some helper functions to let us build the promise.&lt;/p&gt;
&lt;p&gt;We create a deferred object because whether we use ajax or not, we want the consumer to use the promise - even if we &lt;em&gt;can&lt;/em&gt; return straightaway in some circumstances (when we have the name) we can&amp;rsquo;t in all - so the caller must always expect a promise.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;if&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;self&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;name&lt;/span&gt; &lt;span style="color:#f92672"&gt;!==&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;null&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;deferred&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;resolve&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;self&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;name&lt;/span&gt; &lt;span style="color:#f92672"&gt;+&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34; (from Cache!)&amp;#34;&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;If we already have the name, we can just &lt;code&gt;resolve&lt;/code&gt; the deferred object immediately - this is the easy case. I&amp;rsquo;ve added &amp;lsquo;from cache&amp;rsquo; to the name so we can see when it comes from the cache compared to the server.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Tip:&lt;/strong&gt; You can resolve a promise even before you return it. It still works fine for the consumer.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Finally, we can handle the case if we don&amp;rsquo;t already have the name:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;else&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// Get the name from the server.
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;$http&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;get&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;/api/my/name/&amp;#39;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; .&lt;span style="color:#a6e22e"&gt;success&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;self&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;name&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;deferred&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;resolve&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;name&lt;/span&gt; &lt;span style="color:#f92672"&gt;+&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34; (from Server!)&amp;#34;&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; })
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; .&lt;span style="color:#a6e22e"&gt;error&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;response&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;deferred&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;reject&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;response&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; });
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;So if we get success from the server, we can &lt;code&gt;resolve&lt;/code&gt; the promise. Otherwise, we &lt;code&gt;reject&lt;/code&gt; it, which means failure.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Call &lt;code&gt;resolve&lt;/code&gt; on a deferred object to complete it successfully, call &lt;code&gt;reject&lt;/code&gt; to fail it with an error.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Finally, we just return the promise we&amp;rsquo;ve built with &lt;code&gt;deferred&lt;/code&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;deferred&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;promise&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And that&amp;rsquo;s it! You can see it in action below, press &amp;lsquo;Update Name&amp;rsquo; a few times and you&amp;rsquo;ll see it uses the cache.&lt;/p&gt;
&lt;iframe width="100%" height="300" src="http://jsfiddle.net/dwmkerr/LeZU4/embedded/result,html,js" allowfullscreen="allowfullscreen" frameborder="0"&gt;&lt;/iframe&gt;
&lt;p&gt;How do we use this? We&amp;rsquo;ll it&amp;rsquo;s simple, here&amp;rsquo;s a controller that uses the service we&amp;rsquo;ve built:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;app&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;controller&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;MainController&amp;#39;&lt;/span&gt;, &lt;span style="color:#66d9ef"&gt;function&lt;/span&gt; (&lt;span style="color:#a6e22e"&gt;$scope&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;NameService&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// We have a name on the code, but it&amp;#39;s initially empty...
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;$scope&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;name&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;&amp;#34;&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// We have a function on the scope that can update the name.
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;$scope&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;updateName&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;() {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;NameService&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;getName&lt;/span&gt;()
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; .&lt;span style="color:#a6e22e"&gt;then&lt;/span&gt;(
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;/* success function */&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;$scope&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;name&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; },
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;/* error function */&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;result&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;console&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;log&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;Failed to get the name, result is &amp;#34;&lt;/span&gt; &lt;span style="color:#f92672"&gt;+&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;result&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; });
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; };
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;});
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now there&amp;rsquo;s something different here. Before, we might have used the &lt;code&gt;error&lt;/code&gt; or &lt;code&gt;success&lt;/code&gt; function of the promise. But here we use &lt;code&gt;then&lt;/code&gt;. Why is that?&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;success&lt;/code&gt; and &lt;code&gt;error&lt;/code&gt; are functions on a promise that AngularJS adds for us when using &lt;code&gt;$http&lt;/code&gt; or &lt;code&gt;$resource&lt;/code&gt;. They&amp;rsquo;re not standard, you won&amp;rsquo;t find them on other promises.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;So we&amp;rsquo;ve seen how promises work, what they are and so on, now we&amp;rsquo;ll look into this success/error/then stuff.&lt;/p&gt;
&lt;h2 id="promises---success-error-then"&gt;Promises - Success, Error, Then&lt;/h2&gt;
&lt;p&gt;Now we know that &lt;code&gt;$http&lt;/code&gt; returns a promise, and we know that we can call &lt;code&gt;success&lt;/code&gt; or &lt;code&gt;error&lt;/code&gt; on that promise. It would be sensible to think that these functions are a standard part of promise - but they&amp;rsquo;re not!&lt;/p&gt;
&lt;p&gt;When you are using a promise, the function you should call is &lt;code&gt;then&lt;/code&gt;. &lt;code&gt;then&lt;/code&gt; takes two parameters - a callback function for success and a callback function for failure. Taking a look at our original &lt;code&gt;$http&lt;/code&gt; example, we can rewrite it to use this function.
So this code:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;$http&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;get&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;/api/my/name&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; .&lt;span style="color:#a6e22e"&gt;success&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;console&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;log&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;Your name is: &amp;#34;&lt;/span&gt; &lt;span style="color:#f92672"&gt;+&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; })
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; .&lt;span style="color:#a6e22e"&gt;error&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;response&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;status&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;console&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;log&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;The request failed with response &amp;#34;&lt;/span&gt; &lt;span style="color:#f92672"&gt;+&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;response&lt;/span&gt; &lt;span style="color:#f92672"&gt;+&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34; and status code &amp;#34;&lt;/span&gt; &lt;span style="color:#f92672"&gt;+&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;status&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; };
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;becomes:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;$http&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;get&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;/api/my/name&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; .&lt;span style="color:#a6e22e"&gt;then&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;response&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;console&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;log&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;Your name is: &amp;#34;&lt;/span&gt; &lt;span style="color:#f92672"&gt;+&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;response&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;data&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }, &lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;result&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;console&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;log&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;The request failed: &amp;#34;&lt;/span&gt; &lt;span style="color:#f92672"&gt;+&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;result&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; };
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;We &lt;strong&gt;can&lt;/strong&gt; use &lt;code&gt;success&lt;/code&gt; or &lt;code&gt;error&lt;/code&gt; when using &lt;code&gt;$http&lt;/code&gt; - it&amp;rsquo;s convenient. For one thing, the &lt;code&gt;error&lt;/code&gt; function gives us a response and status (and more) and the &lt;code&gt;success&lt;/code&gt; function gives us the response data (rather than the full response object).&lt;/p&gt;
&lt;p&gt;But remember that it&amp;rsquo;s not a standard part of a promise. You can can add your own versions of these functions to promises you build yourself if you want:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;promise&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;success&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;fn&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;promise&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;then&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;response&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;fn&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;response&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;data&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;response&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;status&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;response&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;headers&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;config&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; });
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;promise&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; };
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;promise&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;error&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;fn&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;promise&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;then&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;null&lt;/span&gt;, &lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;response&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;fn&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;response&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;data&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;response&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;status&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;response&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;headers&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;config&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; });
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;promise&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; };
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;this is exactly how angular does it.&lt;/p&gt;
&lt;p&gt;So what&amp;rsquo;s the advice?&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Use &lt;code&gt;success&lt;/code&gt; or &lt;code&gt;error&lt;/code&gt; with &lt;code&gt;$http&lt;/code&gt; promises if you want to - just remember they&amp;rsquo;re not standard, and the parameters are different to those for &lt;code&gt;that&lt;/code&gt; callbacks.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;So if you change your code so that your promise is not returned from &lt;code&gt;$http&lt;/code&gt;, as we did in the earlier example when we load data from a cache, your code will break if you expect &lt;code&gt;success&lt;/code&gt; or &lt;code&gt;error&lt;/code&gt; to be there.&lt;/p&gt;
&lt;p&gt;A safe approach is to use &lt;code&gt;then&lt;/code&gt; wherever possible.&lt;/p&gt;
&lt;h2 id="advanced-promises---chaining"&gt;Advanced Promises - Chaining&lt;/h2&gt;
&lt;p&gt;If you&amp;rsquo;ve had your fill of promises for now, you can skip to &lt;a href="#thefutureofpromises"&gt;The Future of Promises&lt;/a&gt; or &lt;a href="#wrappingup"&gt;Wrapping Up&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;One useful aspect of promises is that the &lt;code&gt;then&lt;/code&gt; function returns the promise itself. This means that you can actually &lt;em&gt;chain&lt;/em&gt; promises, to create conscise blocks of logic that are executed at the appropriate times, without lots of nesting.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s consider an example where we need to fetch the user&amp;rsquo;s name from the backend, but we have to use separate requests to get their profile information and then their application permissions.&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s an example:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;var&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;details&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;username&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;null&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;profile&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;null&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;permissions&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;null&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;};
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;$http&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;get&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;/api/user/name&amp;#39;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; .&lt;span style="color:#a6e22e"&gt;then&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;response&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// Store the username, get the profile.
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;details&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;username&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;response&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;data&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;$http&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;get&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;/api/profile/&amp;#39;&lt;/span&gt; &lt;span style="color:#f92672"&gt;+&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;details&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;username&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; })
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; .&lt;span style="color:#a6e22e"&gt;then&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;response&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// Store the profile, now get the permissions.
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;details&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;profile&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;response&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;data&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;$http&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;get&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;/api/security/&amp;#39;&lt;/span&gt; &lt;span style="color:#f92672"&gt;+&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;details&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;username&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; })
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; .&lt;span style="color:#a6e22e"&gt;then&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;response&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// Store the permissions
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;details&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;permissions&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;response&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;data&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;console&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;log&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;The full user details are: &amp;#34;&lt;/span&gt; &lt;span style="color:#f92672"&gt;+&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;JSON&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;stringify&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;details&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; });
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now we have a series of asynchronous calls that we can coordinate without having lots of nested callbacks.&lt;/p&gt;
&lt;p&gt;We can also greatly simplify error handling - let&amp;rsquo;s see the example again, with an exception thrown in:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;$http&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;get&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;/api/user/name&amp;#39;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; .&lt;span style="color:#a6e22e"&gt;then&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;response&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// Store the username, get the profile.
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;details&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;username&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;response&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;data&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;$http&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;get&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;/api/profile/&amp;#39;&lt;/span&gt; &lt;span style="color:#f92672"&gt;+&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;details&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;username&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; })
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; .&lt;span style="color:#a6e22e"&gt;then&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;response&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// Store the profile, now get the permissions.
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;details&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;profile&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;response&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;data&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;throw&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;Oh no! Something failed!&amp;#34;&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; })
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; .&lt;span style="color:#a6e22e"&gt;then&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;response&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// Store the permissions
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;details&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;permissions&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;response&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;data&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;console&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;log&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;The full user details are: &amp;#34;&lt;/span&gt; &lt;span style="color:#f92672"&gt;+&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;JSON&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;stringify&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;details&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; })
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; .&lt;span style="color:#66d9ef"&gt;catch&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;error&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;console&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;log&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;An error occured: &amp;#34;&lt;/span&gt; &lt;span style="color:#f92672"&gt;+&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;error&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; });
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;We can use &lt;code&gt;catch(callback)&lt;/code&gt; - which is actually just shorthand for &lt;code&gt;then(null, callback)&lt;/code&gt;. There&amp;rsquo;s even a &lt;code&gt;finally&lt;/code&gt; - which is executed whether or not the operations fail or succeed.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Use &lt;code&gt;catch&lt;/code&gt; and for error handling with promises - and use &lt;code&gt;finally&lt;/code&gt; for logic that&amp;rsquo;s executed after success OR failure.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The composition of promises can simplify complicated code - particularly when you add in error handling!&lt;/p&gt;
&lt;p&gt;One final point to make which is not quite related to chaining but does relate to multiple promises is &lt;code&gt;$q.all&lt;/code&gt;. &lt;code&gt;all&lt;/code&gt; can be used to build a single promise from a set of promises.&lt;/p&gt;
&lt;p&gt;You can pass an array of promises to &lt;code&gt;all&lt;/code&gt; and you get back a single promise - which is resolved when all of the promises it contains resolve. This can be useful if you are building complex methods that may have to perform multiple asynchronous tasks - such as multiple ajax calls.&lt;/p&gt;
&lt;h2 id="advanced-promises---routing"&gt;Advanced Promises - Routing&lt;/h2&gt;
&lt;p&gt;There&amp;rsquo;s a particular area of AngularJS that uses promises to great effect, and that&amp;rsquo;s the router.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s imagine we have a router like the following:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;$routeProvider&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; .&lt;span style="color:#a6e22e"&gt;when&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;/home&amp;#39;&lt;/span&gt;, {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;templateUrl&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;home.html&amp;#39;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;controller&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;MainController&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; })
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; .&lt;span style="color:#a6e22e"&gt;when&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;/profile&amp;#39;&lt;/span&gt;, {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;templateUrl&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;profile.html&amp;#39;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;controller&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;ProfileController&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; })
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Here we have two routes. The home route takes us to the home page, with the &lt;code&gt;MainController&lt;/code&gt;, and the profile route takes us to the user&amp;rsquo;s profile page.&lt;/p&gt;
&lt;p&gt;Our ProfileController uses our funky name service:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;app&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;controller&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;ProfileController&amp;#39;&lt;/span&gt;, &lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;$scope&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;NameService&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;$scope&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;name&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;null&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;NameService&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;getName&lt;/span&gt;().&lt;span style="color:#a6e22e"&gt;then&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;$scope&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;name&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; });
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;});
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The problem is, &lt;strong&gt;until the name service gets the name from the backend, the name is null&lt;/strong&gt;. This means if our view binds to the name, it&amp;rsquo;ll flicker - first it&amp;rsquo;s empty then its set.&lt;/p&gt;
&lt;p&gt;What we&amp;rsquo;d like to do is actully say to the router - &amp;ldquo;I&amp;rsquo;m going to go to this view, but only when you can tell me my name&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;We can do this with the &lt;em&gt;resolves&lt;/em&gt; in the router, here&amp;rsquo;s how it works:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// Create a function that uses the NameService
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// to return the getName promise.
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;var&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;getName&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;NameService&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;NameService&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;getName&lt;/span&gt;();
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; };
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;$routeProvider&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; .&lt;span style="color:#a6e22e"&gt;when&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;/home&amp;#39;&lt;/span&gt;, {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;templateUrl&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;/home.html&amp;#39;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;controller&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;MainController&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; })
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; .&lt;span style="color:#a6e22e"&gt;when&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;/profile&amp;#39;&lt;/span&gt;, {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;templateUrl&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;/profile.html&amp;#39;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;controller&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;ProfileController&amp;#39;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;/* only navigate when we&amp;#39;ve resolved these promises */&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;resolve&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;getName&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; })
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;so now we have a &lt;em&gt;resolve&lt;/em&gt; on the route - when we go to the profile page the router will wait until the promise returned by &lt;code&gt;getName&lt;/code&gt; resolves, then it will pass the result into the controller, as the parameter called &lt;code&gt;name&lt;/code&gt;. Now our controller looks like this:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;app&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;controller&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;ProfileController&amp;#39;&lt;/span&gt;, &lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;$scope&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;$scope&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;name&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;});
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Much better! And also &lt;strong&gt;much&lt;/strong&gt; more testable.&lt;/p&gt;
&lt;p&gt;One thing you may wonder - why do I use &lt;code&gt;getName&lt;/code&gt; as the resolve function instead of just using &lt;code&gt;NameService.getName&lt;/code&gt; directly?&lt;/p&gt;
&lt;p&gt;That&amp;rsquo;s because the route is set up in a &lt;code&gt;config&lt;/code&gt; function - and that function cannot have services injected. However, a resolve function &lt;strong&gt;can&lt;/strong&gt;, so we just use a function and let AngularJS inject the &lt;code&gt;NameService&lt;/code&gt; for us.&lt;/p&gt;
&lt;p&gt;Now for an important statement:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;If the first thing your controller does is fetch data from the server, it&amp;rsquo;s probably wrong.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Why? Because if your controller needs data, inject it - let the router ensure the data is ready. Then you don&amp;rsquo;t have controllers in an invalid state as they&amp;rsquo;re loading - and your controllers become easier to test.&lt;/p&gt;
&lt;p&gt;Be aware of &lt;code&gt;resolve&lt;/code&gt; for routes - it&amp;rsquo;s a great way to handle loading of required data, authentication and other things that you might be putting into the wrong place.&lt;/p&gt;
&lt;p&gt;You can see the example above in action here:&lt;/p&gt;
&lt;iframe width="100%" height="300" src="http://jsfiddle.net/dwmkerr/m29pe/embedded/result,js,html" allowfullscreen="allowfullscreen" frameborder="0"&gt;&lt;/iframe&gt;
&lt;p&gt;What&amp;rsquo;s cool is we can also see our caching logic by going to and from the Home and Profile pages. The promises are keeping our code clean and testable.&lt;/p&gt;
&lt;p&gt;As a final note on promises when routing, you can specify multiple resolves if you need to:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;$routeProvider&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; .&lt;span style="color:#a6e22e"&gt;when&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;/profile&amp;#39;&lt;/span&gt;, {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;templateUrl&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;/profile.html&amp;#39;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;controller&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;ProfileController&amp;#39;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;/* only navigate when we&amp;#39;ve resolved these promises */&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;resolve&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;getName&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;profile&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;getProfile&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;anythingElse&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;getAnythingElse&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; })
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;in this case each resolve is injected into the controller.&lt;/p&gt;
&lt;h2 id="advanced-promises---tips--tricks"&gt;Advanced Promises - Tips &amp;amp; Tricks&lt;/h2&gt;
&lt;p&gt;This section just contains some tips and tricks you might find useful when working with promises.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Promises in directives are not resolved automatically since AngularJS 1.2. Previously, if you passed a promise to a directive with an &amp;lsquo;=&amp;rsquo; binding, AngularJS would resolve the promise for you, this is no longer the case.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id="the-future-of-promises"&gt;The Future of Promises&lt;/h2&gt;
&lt;p&gt;So promises are a core part of AngularJS and to use the framework effectively, you must understand how to use them and how they work. But what is the future of promises?&lt;/p&gt;
&lt;p&gt;It&amp;rsquo;s almost certain that promises are going to become a &lt;strong&gt;native&lt;/strong&gt; feature of JavaScript, they are part of the proposed ECMAScript 6 specification.&lt;/p&gt;
&lt;p&gt;The functionality of the &lt;code&gt;q&lt;/code&gt; library and AngularJS&amp;rsquo; implementation of promises are very similar indeed to the proposed specification, but be aware that when promises become standard, AngularJS is most likely to adapt their own promises to work like native promises.&lt;/p&gt;
&lt;p&gt;You can read more at &lt;a href="http://www.html5rocks.com/en/tutorials/es6/promises/"&gt;html5rocks.com/en/tutorials/es6/promises/&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Just be aware that you&amp;rsquo;ll see promises more and more, in other frameworks and in vanilla JavaScript.&lt;/p&gt;
&lt;h2 id="wrapping-up"&gt;Wrapping Up&lt;/h2&gt;
&lt;p&gt;I hope this post has been useful to understanding promises. Any feedback is always good, so let me know if anything is unclear or could be improved. To finish this article, here are some useful links:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;The Q library&lt;/strong&gt; &lt;a href="https://github.com/kriskowal/q"&gt;github.com/kriskowal/q&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The AngularJS &lt;code&gt;$q&lt;/code&gt; Service&lt;/strong&gt; &lt;a href="https://docs.angularjs.org/api/ng/service/$q"&gt;docs.angularjs.org/api/ng/service/$q&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Promises in ECMAScript 6&lt;/strong&gt; &lt;a href="http://www.html5rocks.com/en/tutorials/es6/promises/"&gt;html5rocks.com/en/tutorials/es6/promises/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;XmlHttpRequest, which we used in an example&lt;/strong&gt; &lt;a href="https://developer.mozilla.org/en/docs/Web/API/XMLHttpRequest"&gt;developer.mozilla.org/en/docs/Web/API/XMLHttpRequest&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;And also some interesting discussions:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://spion.github.io/posts/why-i-am-switching-to-promises.html"&gt;Why I am switching to promises&lt;/a&gt; - Written by Gorgi Kosev, great article describing why a switch from callbacks to promises can be a very good thing in NodeJS applications.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blog.ometer.com/2011/07/24/callbacks-synchronous-and-asynchronous/"&gt;Callbacks, sychronous and asynchronous&lt;/a&gt; - From Havoc, this post contains many useful points for API writers who are using callbacks or promises. One key takeaway is to &lt;strong&gt;never&lt;/strong&gt; do what a sample in this article does which is resolve a promise either synchronously or asynchronously, as it leads to code which can be difficult to reason about. I&amp;rsquo;ll be mentioning this more in a later update which will explain the problem and solution.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blog.izs.me/post/59142742143/designing-apis-for-asynchrony"&gt;Designing for Asynchrony&lt;/a&gt; - Written by Isaac Z. Schlueter, this post is another great one for API designers that takes a look into asynchrony.&lt;/p&gt;</description><category>CodeProject</category></item><item><title>Practical AngularJS Part 2</title><link>https://dwmkerr.com/practical-angularjs-part-2/</link><pubDate>Wed, 19 Feb 2014 15:29:29 +0000</pubDate><guid>https://dwmkerr.com/practical-angularjs-part-2/</guid><description>&lt;p&gt;I&amp;rsquo;m going to be working in F# almost exclusively for a short while, so before I throw myself into that I wanted to wind up my Practical AngularJS Part 2 article. It&amp;rsquo;s ready to rock here:&lt;/p&gt;
&lt;p&gt;&lt;a title="Practical AngularJS Part 2 – Components of an AngularJS Application" href="http://www.dwmkerr.com/practical-angularjs-part2/"&gt;Practical AngularJS Part 2 - Components of an AngularJS Application&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;In this article we get a brief introduction to what&amp;rsquo;s in the toolkit for an angular developers - filters, controllers, services, directives, views and routes. I don&amp;rsquo;t go into too much detail, we&amp;rsquo;re just seeing what the different components are. Spread the word, share the article and as always, comments are welcome.&lt;/p&gt;</description><category>CodeProject</category></item><item><title>Langton's Ant in Javascript</title><link>https://dwmkerr.com/langtons-ant-in-javascript/</link><pubDate>Sun, 15 Dec 2013 10:47:18 +0000</pubDate><guid>https://dwmkerr.com/langtons-ant-in-javascript/</guid><description>&lt;p&gt;Langton&amp;rsquo;s Ant is a great simulation to write to play with a language. Just today I&amp;rsquo;ve completed my Langton&amp;rsquo;s Ant write up and published it on the CodeProject, you can see the article at &lt;a title="Learn Javascript Part 3 - AngularJS and Langton's Ant" href="http://www.codeproject.com/Articles/696943/Learn-JavaScript-Part-3-AngularJS-and-Langtons-Ant" target="_blank"&gt;Learn JavaScript Part 3 - Angularjs and Langton&amp;rsquo;s Ant&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a title="Langton's Ant" href="http://www.dwmkerr.com/experiments/langtonsant/" target="_blank"&gt;&lt;img src="images/langtonsant.jpg" alt="Langton's Ant" width="640" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;There are some interesting things in the article for angular too - a look at using directives for custom elements, how to handle both the DOM and Angular loading correctly, and timers and intervals. This is the third part on my series on Learn Javascript, the next will focus on NodeJS. Enjoy, share, fork and comments are always welcome.&lt;/p&gt;
&lt;p&gt;The default configuration of the simulation is interesting - try a simulation of the form &amp;rsquo;left, left, right, right&amp;rsquo; for another type of behaviour.&lt;/p&gt;</description><category>CodeProject</category></item><item><title>Introducing Practical AngularJS</title><link>https://dwmkerr.com/introducing-practical-angularjs/</link><pubDate>Mon, 25 Nov 2013 16:16:09 +0000</pubDate><guid>https://dwmkerr.com/introducing-practical-angularjs/</guid><description>&lt;p&gt;I was recently at Devoxx in Antwerp, primarily because I wanted to get involved in some of the sessions that were being hosted by guys from the AngularJS team at Google. I&amp;rsquo;ve had a chance to work a little with Backbone and KnockoutJS and had been recently deliberately holding off looking at AngularJS so I could hit the conference and workshops fresh and unencumbered with any preconceptions.&lt;/p&gt;
&lt;p&gt;The sessions were great, and since then I&amp;rsquo;ve been working on a couple of projects that use Angular. As I&amp;rsquo;ve always found writing about a topic a great way to really cement what you know about it, and to understand where the holes in your knowledge are, I&amp;rsquo;ve started a new series of articles called &amp;lsquo;&lt;a title="Practical AngularJS" href="http://www.dwmkerr.com/practical-angularjs/"&gt;Practical AngularJS&lt;/a&gt;&amp;rsquo;.&lt;/p&gt;
&lt;p&gt;This series is a little different to others I&amp;rsquo;m working on at the moment (such as the seemingly &lt;a title=".NET Shell Extensions - Shell Context Menus" href="http://www.codeproject.com/Articles/512956/NET-Shell-Extensions-Shell-Context-Menus" target="_blank"&gt;endless SharpShell articles&lt;/a&gt; and &lt;a title="Space Invaders" href="http://www.codeproject.com/Articles/681130/Learn-JavaScript-Part-2-Space-Invaders" target="_blank"&gt;Learn JavaScript&lt;/a&gt; which is taking some time) as I&amp;rsquo;m writing it on my own blog rather than on the CodeProject. The reason behind this is purely push me into getting better at handling my blog (particularly the code samples) and because the code I&amp;rsquo;m writing doesn&amp;rsquo;t need to be associated with download links and so on, it can all be in fiddles.&lt;/p&gt;
&lt;p&gt;Anyway, enough chatter - part one of Practical AngularJS is now finished. It&amp;rsquo;s a new series, so it&amp;rsquo;s early enough to have a say in where it goes, please comment and share and let me kn0w whether you find it useful, pointless, or anything in-between.&lt;/p&gt;
&lt;p&gt;As a short teaser, in &lt;a title="Practical AngularJS Part 1 – Introducing AngularJS" href="http://www.dwmkerr.com/practical-angularjs-part1" target="_blank"&gt;Practical AngularJS Part 1 - Introducing AngularJS&lt;/a&gt; we take a look at what AngularJS is, why we&amp;rsquo;d consider using it and when. We start out with a trivial task for a web application, and see how it quickly becomes a bit sluggish and painful to do certain things, then see how using AnguarJS can ease that pain, letting us focus on the important stuff and it help out with the grunt work. We take our initially messy app and make it a lot more manageable, and look into how we can with our new structure start to write unit tests for the logic.&lt;/p&gt;
&lt;p&gt;So this is the bulk of Part 1. Unless there&amp;rsquo;s a strong push for another topic, Part 2 will focus on testing. Testing is core to the development ideals of the AngularJS project and is something that it was built in mind for. We&amp;rsquo;ll look into how quickly we can write unit tests, and the flexibility that we&amp;rsquo;ve got.&lt;/p&gt;
&lt;p&gt;Rather than advocating patterns such as BDD or TDD, we&amp;rsquo;ll again shy away from theoretical discussions about what is conceptually the best paradigm and just dig in - playing with the code and seeing what works and what doesn&amp;rsquo;t. By the end of it you&amp;rsquo;ll have a good idea of the freedom you&amp;rsquo;ve got with testing - leaving it up to you to choose the pattern or process that fits your style, or the style of your team and project the best.&lt;/p&gt;</description><category>CodeProject</category></item></channel></rss>