Loading Client Side Templates in Angular JS

One of the features of Angular JS is client routing. This enables us to create single page applications (SPA) by which our site loads an initial html page and every subsequent page is brought in dynamically without triggering a full page refresh.

I was giving a presentation on Angular recently and someone in the audience asked if the templates where downloaded when the web app started, or dynamically as the user navigates to a specific page. This is a great question because on a complex multi-page site, you don’t want to load the templates upfront.

So I decided to investigate. Fortunately, this was an easy thing to do using the Chrome Dev Tools. As part of my talk I built a small sample app called Tweet Save. This web app allows you to search twitter and then bookmark tweets you like. You can see it’s source on github.

The saved tweets page relies on a template called “SavedTweets.html.” Inspecting the network tab on initial load doesn’t show any reference to SavedTweets.html. After clicking the link to the saved tweets page I see the following in my dev tools:

Dev tools

The template is only loaded when the user requests that route from my route provider. If navigate back to the initial search page, then again to saved tweets the two files above will not be downloaded again. That’s because at this point, angular already has the template it needs for that route.

so there we have it. Angular only loads html templates on demand when a user requests that specific route.

Advertisements

Nested Directive Virtual Groups in Angular JS

In my last post I went over a new unreleased feature of Angular JS, Directive Virtual Groups. This allows you to append -start to a directive allowing it to span multiple DOM elements. You can terminate the multi-DOM element by applying -end to the same directive. For example:

<tr ng-repeat-start="order in orders">
  <td>{{order.description}}</td>
</tr>
<tr ng-repeat-end>
  <td>{{order.number}}</td>
  <td>{{order.price}}</td>
</tr>

This would iterate over a series of orders and render two TR tags for each order. The ng-repeat-start indicates the ng-repeat condition as well as a starting flag for angular. Everything following that (including it’s TR tag) is part of the template, until it reaches a ng-repeat-end. That element is then included and is considered the terminating element.

Well what about multiple -start and -end tags nested? I decided to try to play around with ng-click in addition to ng-repeat. Imagine if we wanted to make only the number and price table cells clickable. We want those cells to call a function named ‘calculate’. I could easily modify the above example to be:

<tr ng-repeat-start="order in orders">
  <td>{{order.description}}</td>
</tr>
<tr ng-repeat-end>
  <td ng-click-start="calculate(order)">{{order.number}}</td>
  <td ng-click-end>{{order.price}}</td>
</tr>

While this may seem complicated, the syntax basically says

  1. start the repeat process at the first TR tag
  2. terminate the template at the second tag (where ng-repeat-end) comes in
  3. In the second TR tag, start defining a clickable region ng-click-start
  4. Apply click region to first and second TD until ng-click-end is found

Notice how I only defined the target of the ng-click once, but both elements are now clickable and will call the calculate method. Pretty cool and DRY!

As a reminder, this feature is in the code base but has NOT made it to the stable release of Angular. So it might not work for you unless you have the latest build form their Github repository.
works on my machine

Angular JS Directives – Repeating over multiple elements

I was watching a video from NDC featuring a cage match between Ember JS and Angular JS. Tom Dale and Rob Connery both did a good job of representing Ember and Angular respectively. At one point, Tom showed a feature of handlebars (which Ember uses for templating) that allowed him to iterate over a javascript array and for each element render out two html TR (table row) tags. This is easy to do in Ember because handlerbars uses a #each template and you can basically put anything you want between #each and /each like so (tom’s code from the talk follows):

{{#each orders}}
  <tr>
    <td>{{description}}</td>
  </tr>
 
  <tr>
    <td>{{number}}</td>
    <td>{{currency price}}</td>
  </tr>
{{/each}}

The result is something that looks like this (screenshot from the video):

Ember JS each example

So for each item, show the description on one line and the number and price (formatted as currency) on the next line. Pretty standard stuff for simple tabular data.

When it came time for Rob to offer rebuttal and duplicate this with Angular, he struggled and I don’t blame him. Even though I’ve done a bit of Angular and given presentations on the topic, while watching this I was stumped. You see Angular has an ng-repeat directive that allows you to iterate over a collection, much like #each. However, ng-repeat is applied to a DOM element and that element itself is used as the template for every item. To illustrate, check out this simple ng-repeat example:

<tr ng-repeat="order in orders">
  <td>{{order.description}}</td>
  <td>{{order.number}}</td>
  <td>{{order.price}}</td>
</tr>

Here we ask angular to iterate over all “orders” and for each iteration place the current item in a variable called “order”. The TR tag itself will be used as a template and anything inside of it will be part of that template also. The problem now is how do you extend this to multiple TR tags like Tom did in the Ember example? It’s not something I had seen before. So I decided to do some research and see if there was in fact a way to do this in Angular.

I came across a few posts that talked about adding the ability to use ng-repeat in an html comment (similar to how Knockout JS bindings can be used in comments), but the Angular lead turned that proposal down. I then found this page on Angular’s Git hub Page. It talks about a new feature of Angular, Directive Virtual Groups. This exactly what I needed ! 🙂

So how does it work? In a nutshell it’s a way to have a directive span over multiple elements by indicating a start and end. This is done by simply appending -start and -end to the directive itself. It’s actually a very nice way of doing this as it extends existing directives.

Unfortunately this isn’t the stable release of Angular, but it’s good to know that this feature is in the code base and is coming. So to play around with it I had to download the  Angular source code and build it on my machine. After that I used the newly built angular.min.js file in a sample application and I was able to write code like this:

 <tr ng-repeat-start="order in orders">
    <td>{{order.description}}</td>
  </tr>
  <tr ng-repeat-end>
    <td>{{order.number}}</td>
    <td>{{order.price}}</td>
  </tr>

This new sytax allows us to take the repeat binding and extend it over two html elements, or any number of elements between -start and -end. If I included a 3rd row in between those TR’s, it would rendered as well.

I think this is a great addition to the framework and I love how it’s implemented as an extension to directives and really highlights the good architecture and separation of concerns you can get. Directives are complicated, but what they do offer is a clean way to isolate functionality and to build reusable components that can be composed on a page, giving the application developer greater power and flexibility