Recipes with Angular.js cover page
Recipes with Angular.js

Recipes with Angular.js

Table of Contents

{{frontmatter}} # Preface

Introduction

Angular.js is an open-source Javascript MVC (Model-View-Controller) framework developed by Google. It gives Javascript developers a highly structured approach to developing rich browser-based applications which, leads to very high productivity.

If you are using Angular.js, or considering it, this cookbook provides easy-to-follow recipes for issues you are likely to face. Each recipe solves a specific problem and provides a solution and in-depth discussion of it.

Code Examples

All code examples in this book can be found on Github.

How to contact me

If you have questions or comments please get in touch with:

Frederik Dietz (fdietz@gmail.com)

Acknowledgements

Special thanks go to my english editor and friend Robert William Smales!

Thanks go to John Lindquist for his excellent screencast project Egghead IO, Lukas Ruebbelke for his awesome videos, Matias Niemela for his great blog. And of course the whole development team behind Angular.js!

{{mainmatter}}

An Introduction to Angular.js

Including the Angular.js Library Code in an HTML Page

Problem

You wish to use Angular.js on a web page.

Solution

In order to get your first Angular.js app up and running you need to include the Angular Javascript file via script tag and make use of the ng-app directive.

 1 <html>
 2   <head>
 3     <script src="http://ajax.googleapis.com/ajax/libs/
 4       angularjs/1.0.4/angular.js">
 5     </script>
 6   </head>
 7   <body ng-app>
 8     <p>This is your first angular expression: {{ 1 + 2 }}</p>
 9   </body>
10 </html>

Tip: You can check out a complete example on github.

Discussion

Adding the ng-app directive tells Angular to kick in its magic. The expression {{ 1 + 2 }} is evaluated by Angular and the result 3 is rendered. Note that removing ng-app will result in the browser rendering the expression as is instead of evaluating it. Play around with the expression! You can, for instance, concatenate Strings and invert or combine Boolean values.

For reasons of brevity we will skip the boilerplate code in the following recipes.

Binding a Text Input to an Expression

Problem

You want user input to be used in another part of your HTML page.

Solution

Use Angulars ng-model directive to bind the text input to the expression.

1 Enter your name: <input type="text" ng-model="name"></input>
2 <p>Hello {{name}}!</p>

You can find the complete example on github.

Discussion

Assigning “name” to the ng-model attribute and using the name variable in an expression will keep both in sync automatically. Typing in the text input will automatically reflect these changes in the paragraph element below.

Consider how you would implement this traditionally using jQuery:

 1 <html>
 2   <head>
 3     <script src="http://code.jquery.com/jquery.min.js"></script>
 4   </head>
 5   <body>
 6     Enter your name: <input type="text"></input>
 7     <p id="name"></p>
 8 
 9     <script>
10       $(document).ready(function() {
11         $("input").keypress(function() {
12           $("#name").text($(this).val());
13         });
14       });
15     </script>
16 
17   </body>
18 </html>

On document ready we bind to the keypress event in the text input and replace the text in the paragraph in the callback function. Using jQuery you need to deal with document ready callbacks, element selection, event binding and the context of this. Quite a lot of concepts to swallow and lines of code to maintain!

Responding to Click Events using Controllers

Problem

You wish to hide an HTML element on button click.

Solution

Use the ng-hide directive in conjunction with a controller to change the visibility status on button click.

 1 <html>
 2   <head>
 3     <script src="js/angular.js"></script>
 4     <script src="js/app.js"></script>
 5     <link rel="stylesheet" href="css/bootstrap.css">
 6   </head>
 7   <body ng-app>
 8     <div ng-controller="MyCtrl">
 9       <button ng-click="toggle()">Toggle</button>
10       <p ng-show="visible">Hello World!</p>
11     </div>
12   </body>
13 </html>

And the controller in js/app.js:

1 function MyCtrl($scope) {
2   $scope.visible = true;
3 
4   $scope.toggle = function() {
5     $scope.visible = !$scope.visible;
6   };
7 }

You can find the complete example on github.

When toggling the button the “Hello World” paragraph will change its visibility.

Discussion

Using the ng-controller directive, we bind the div element including its children to the context of the MyCtrl controller. The ng-click directive will call the toggle() function of our controller on button click. Note that the ng-show directive is bound to the visible scope variable and will toggle the paragraph’s visibility accordingly.

The controller implementation defaults the visible attribute to true and toggles its Boolean state in the toggle function. Both the visible variable and the toggle function are defined on the $scope service which is passed to all controller functions automatically via dependency injection.

The next chapter will go into all the details of controllers in Angular. For now let us quickly discuss the MVVM (Model-View-ViewModel) pattern as used by Angular. In the MVVM pattern the model is plain Javascript, the view is the HTML template and the ViewModel is the glue between the template and the model. The ViewModel makes Angular’s two-way binding possible where changes in the model or the template are in sync automatically.

In our example, the visible attribute is the model, but it could of course be much more complex , when for example retrieving data from a backend service. The controller is used to define the scope which represents the ViewModel. It interacts with the HTML template by binding the scope variable visible and the function toggle() to it.

Converting Expression Output with Filters

Problem

When presenting data to the user, you might need to convert the data to a more user-friendly format. In our case we want to uppercase the name value from the previous recipe in the expression.

Solution

Use the uppercase Angular filter.

1 Enter your name: <input type="text" ng-model="name"></input>
2 <p>Hello {{name | uppercase }}!</p>

You can find the complete example on github.

Discussion

Angular uses the | (pipe) character to combine filters with variables in expressions. When evaluating the expression, the name variable is passed to the uppercase filter. This is similar to working with the Unix bash pipe symbol where an input can be transformed by another program. Also try the lowercase filter!

Creating Custom HTML Elements with Directives

Problem

You wish to render an HTML snippet but hide it conditionally.

Solution

Create a custom directive which renders your Hello World snippet.

1 <body ng-app="MyApp">
2   <label for="checkbox">
3     <input id="checkbox" type="checkbox" ng-model="visible">Toggle me
4   </label>
5   <div show="visible">
6     <p>Hello World</p>
7   </div>
8 </body>

The show attribute is our directive, with the following implementation:

 1 var app = angular.module("MyApp", []);
 2 
 3 app.directive("show", function() {
 4   return {
 5     link: function(scope, element, attributes) {
 6       scope.$watch(attributes.show, function(value){
 7         element.css('display', value ? '' : 'none');
 8       });
 9     }
10   };
11 });

The browser will only show the “Hello World” paragraph if the checkbox is checked and will hide it otherwise.

You can find the complete example on github.

Discussion

Let us ignore the app module creation for now and look into it in more depth in a later chapter. In our example it is just the means to create a directive. Note that the show String is name of the directive which corresponds to the show HTML attribute used in the template.

The directive implementation returns a link function which defines the directive’s behavior. It gets passed the scope, the HTML element and the HTML attributes. Since we passed the visible variable as an attribute to our show directive, we can access it directly via attributes.show. But, since we want to respond to visible variable changes automatically we have to use the $watch service, which provides us with a function callback whenever the value changes. In this callback we change the CSS display property to either blank or none to hide the HTML element.

This was just a small glimpse of what can be achieved with directives and there is a whole chapter dedicated to them that goes into much more detail.

Controllers

Controllers in Angular provide the business logic to handle view behavior, for example responding to a user clicking a button or entering some text in a form. Additionally, controllers prepare the model for the view template.

As a general rule, a controller should not reference or manipulate the DOM directly. This has the benefit of simplifying unit testing controllers.

Assigning a Default Value to a Model

Problem

You wish to assign a default value to the scope in the controller’s context.

Solution

Use the ng-controller directive in your template:

1 <div ng-controller="MyCtrl">
2   <p>{{value}}</p>
3 </div>

Next, define the scope variable in your controller function:

1 var MyCtrl = function($scope) {
2   $scope.value = "some value";
3 };

You can find the complete example on github.

Discussion

Depending on where you use the ng-controller directive, you define its assigned scope. The scope is hierarchical and follows the DOM node hierarchy. In our example, the value expression is correctly evaluated to some value, since value is set in the MyCtrl controller. Note that this would not work if the value expression were moved outside the controllers scope:

1 <p>{{value}}</p>
2 
3 <div ng-controller="MyCtrl">
4 </div>

In this case {{value}} will simply be not rendered at all due to the fact that expression evaluation in Angular.js is forgiving for undefined and null values.

Changing a Model Value with a Controller Function

Problem

You wish to increment a model value by 1 using a controller function.

Solution

Implement an increment function that changes the scope.

1 function MyCtrl($scope) {
2   $scope.value = 1;
3 
4   $scope.incrementValue = function(increment) {
5     $scope.value += increment;
6   };
7 }

This function can be directly called in an expression, in our example we use ng-init:

1 <div ng-controller="MyCtrl">
2   <p ng-init="incrementValue(1)">{{value}}</p>
3 </div>

You can find the complete example on github.

Discussion

The ng-init directive is executed on page load and calls the function incrementValue defined in MyCtrl. Functions are defined on the scope very similar to values but must be called with the familiar parenthesis syntax.

Of course, it would have been possible to increment the value right inside of the expression with value = value +1 but imagine the function being much more complex! Moving this function into a controller separates our business logic from our declarative view template and we can easily write unit tests for it.

Encapsulating a Model Value with a Controller Function

Problem

You wish to retrieve a model via function (instead of directly accessing the scope from the template) that encapsulates the model value.

Solution

Define a getter function that returns the model value.

1 function MyCtrl($scope) {
2   $scope.value = 1;
3 
4   $scope.getIncrementedValue = function() {
5     return $scope.value + 1;
6   };
7 }

Then in the template we use an expression to call it:

1 <div ng-controller="MyCtrl">
2   <p>{{getIncrementedValue()}}</p>
3 </div>

You can find the complete example on github.

Discussion

MyCtrl defines the getIncrementedValue function, which uses the current value and returns it incremented by one. One could argue that depending on the use case it would make more sense to use a filter. But there are use cases specific to the controllers behavior where a generic filter is not required.

Responding to Scope Changes

Problem

You wish to react on a model change to trigger some further actions. In our example we simply want to set another model value depending on the value we are listening to.

Solution

Use the $watch function in your controller.

1 function MyCtrl($scope) {
2   $scope.name = "";
3 
4   $scope.$watch("name", function(newValue, oldValue) {
5     if ($scope.name.length > 0) {
6       $scope.greeting = "Greetings " + $scope.name;
7     }
8   });
9 }

In our example we use the text input value to print a friendly greeting.

1 <div ng-controller="MyCtrl">
2   <input type="text" ng-model="name" placeholder="Enter your name">
3   <p>{{greeting}}</p>
4 </div>

The value greeting will be changed whenever there’s a change to the name model and the value is not blank.

You can find the complete example on github.

Discussion

The first argument name of the $watch function is actually an Angular expression, so you can use more complex expressions (for example: [value1, value2] | json) or even a Javascript function. In this case you need to return a string in the watcher function:

1 $scope.$watch(function() {
2   return $scope.name;
3 }, function(newValue, oldValue) {
4   console.log("change detected: " + newValue)
5 });

The second argument is a function which gets called whenever the expression evaluation returns a different value. The first parameter is the new value and the second parameter the old value. Internally, this uses angular.equals to determine equality which means both objects or values pass the === comparison.

Sharing Models Between Nested Controllers

Problem

You wish to share a model between a nested hierarchy of controllers.

Solution

Use Javascript objects instead of primitives or direct $parent scope references.

Our example template uses a controller MyCtrl and a nested controller MyNestedCtrl:

 1 <body ng-app="MyApp">
 2   <div ng-controller="MyCtrl">
 3     <label>Primitive</label>
 4     <input type="text" ng-model="name">
 5 
 6     <label>Object</label>
 7     <input type="text" ng-model="user.name">
 8 
 9     <div class="nested" ng-controller="MyNestedCtrl">
10       <label>Primitive</label>
11       <input type="text" ng-model="name">
12 
13       <label>Primitive with explicit $parent reference</label>
14       <input type="text" ng-model="$parent.name">
15 
16       <label>Object</label>
17       <input type="text" ng-model="user.name">
18     </div>
19   </div>
20 </body>

The app.js file contains the controller definition and initializes the scope with some defaults:

 1 var app = angular.module("MyApp", []);
 2 
 3 app.controller("MyCtrl", function($scope) {
 4   $scope.name = "Peter";
 5   $scope.user = {
 6     name: "Parker"
 7   };
 8 });
 9 
10 app.controller("MyNestedCtrl", function($scope) {
11 });

Play around with the various input fields and see how changes affect each other.

You can find the complete example on github.

Discussion

All the default values are defined in MyCtrl which is the parent of MyNestedCtrl. When making changes in the first input field, the changes will be in sync with the other input fields bound to the name variable. They all share the same scope variable as long as they only read from the variable. If you change the nested value, a copy in the scope of the MyNestedCtrl will be created. From now on, changing the first input field will only change the nested input field which explicitly references the parent scope via $parent.name expression.

The object-based value behaves differently in this regard. Whether you change the nested or the MyCtrl scopes input fields, the changes will stay in sync. In Angular, a scope prototypically inherits properties from a parent scope. Objects are therefore references and kept in sync. Whereas primitive types are only in sync as long they are not changed in the child scope.

Generally I tend to not use $parent.name and instead always use objects to share model properties. If you use $parent.name the MyNestedCtrl not only requires certain model attributes but also a correct scope hierarchy to work with.

Tip: The Chrome plugin Batarang simplifies debugging the scope hierarchy by showing you a tree of the nested scopes. It is awesome!

Sharing Code Between Controllers using Services

Problem

You wish to share business logic between controllers.

Solution

Utilise a Service to implement your business logic and use dependency injection to use this service in your controllers.

The template shows access to a list of users from two controllers:

1 <div ng-controller="MyCtrl">
2   <ul ng-repeat="user in users">
3     <li>{{user}}</li>
4   </ul>
5   <div class="nested" ng-controller="AnotherCtrl">
6     First user: {{firstUser}}
7   </div>
8 </div>

The service and controller implementation in app.js implements a user service and the controllers set the scope initially:

 1 var app = angular.module("MyApp", []);
 2 
 3 app.factory("UserService", function() {
 4   var users = ["Peter", "Daniel", "Nina"];
 5 
 6   return {
 7     all: function() {
 8       return users;
 9     },
10     first: function() {
11       return users[0];
12     }
13   };
14 });
15 
16 app.controller("MyCtrl", function($scope, UserService) {
17   $scope.users = UserService.all();
18 });
19 
20 app.controller("AnotherCtrl", function($scope, UserService) {
21   $scope.firstUser = UserService.first();
22 });

You can find the complete example on github.

Discussion

The factory method creates a singleton UserService, that returns two functions for retrieving all users and the first user only. The controllers get the UserService injected by adding it to the controller function as params.

Using dependency injection here is quite nice for testing your controllers, since you can easily inject a UserService stub. The only downside is that you can’t minify the code from above without breaking it, since the injection mechanism relies on the exact string representation of UserService. It is therefore recommended to define dependencies using inline annotations, which keeps working even when minified:

1 app.controller("AnotherCtrl", ["$scope", "UserService",
2   function($scope, UserService) {
3     $scope.firstUser = UserService.first();
4   }
5 ]);

The syntax looks a bit funny, but since strings in arrays are not changed during the minification process it solves our problem. Note that you could change the parameter names of the function, since the injection mechanism relies on the order of the array definition only.

Another way to achieve the same is using the $inject annotation:

1 var anotherCtrl = function($scope, UserService) {
2   $scope.firstUser = UserService.first();
3 };
4 
5 anotherCtrl.$inject = ["$scope", "UserService"];

This requires you to use a temporary variable to call the $inject service. Again, you could change the function parameter names. You will most probably see both versions applied in apps using Angular.

Testing Controllers

Problem

You wish to unit test your business logic.

Solution

Implement a unit test using Jasmine and the angular-seed project. Following our previous $watch recipe, this is how our spec would look.

 1 describe('MyCtrl', function(){
 2   var scope, ctrl;
 3 
 4   beforeEach(inject(function($controller, $rootScope) {
 5     scope = $rootScope.$new();
 6     ctrl = $controller(MyCtrl, { $scope: scope });
 7   }));
 8 
 9   it('should change greeting value if name value is changed', function() {
10     scope.name = "Frederik";
11     scope.$digest();
12     expect(scope.greeting).toBe("Greetings Frederik");
13   });
14 });

You can find the complete example on github.

Discussion

Jasmine specs use describe and it functions to group specs and beforeEach and afterEach to setup and teardown code. The actual expectation compares the greeting from the scope with our expectation Greetings Frederik.

The scope and controller initialization is a bit more involved. We use inject to initialize the scope and controller as closely as possible to how our code would behave at runtime too. We can’t just initialize the scope as a Javascript object {} since we would then not be able to call $watch on it. Instead $rootScope.$new() will do the trick. Note that the $controller service requires MyCtrl to be available and uses an object notation to pass in dependencies.

The $digest call is required in order to trigger a watch execution after we have changed the scope. We need to call $digest manually in our spec whereas at runtime Angular will do this for us automatically.

Directives

Directives are one of the most powerful concepts in Angular since they let you invent new HTML elements specific to your application. This allows you to create reusable components which encapsulate complex DOM structures, stylesheets and even behavior.

Enabling/Disabling DOM Elements Conditionally

Problem

You wish to disable a button depending on a checkbox state.

Solution

Use the ng-disabled directive and bind its condition to the checkbox state.

1 <body ng-app>
2   <label><input type="checkbox" ng-model="checked"/>Toggle Button</label>
3   <button ng-disabled="checked">Press me</button>
4 </body>

You can find the complete example on github.

Discussion

The ng-disabled directive is a direct translation from the disabled HTML attribute, without you needing to worry about browser incompatibilities. It is bound to the checked model using an attribute value as is the checkbox using the ng-model directive. In fact the checked attribute value is again an Angular expression. You could for example invert the logic and use !checked instead.

This is just one example of a directive shipped with Angular. There are many others like for example ng-hide, ng-checked or ng-mouseenter and I encourage you go through the API Reference and explore all the directives Angular has to offer.

In the next recipes we will focus on implementing directives.

Changing the DOM in Response to User Actions

Problem

You wish to change the CSS of an HTML element on a mouse click and encapsulate this behavior in a reusable component.

Solution

Implement a directive my-widget that contains an example paragraph of text we want to style.

1 <body ng-app="MyApp">
2   <my-widget>
3     <p>Hello World</p>
4   </my-widget>
5 </body>

We use a link function in our directive implementation to change the CSS of the paragraph.

 1 var app = angular.module("MyApp", []);
 2 
 3 app.directive("myWidget", function() {
 4   var linkFunction = function(scope, element, attributes) {
 5     var paragraph = element.children()[0];
 6     $(paragraph).on("click", function() {
 7       $(this).css({ "background-color": "red" });
 8     });
 9   };
10 
11   return {
12     restrict: "E",
13     link: linkFunction
14   };
15 });

When clicking on the paragraph the background color changes to red.

You can find the complete example on github.

Discussion

In the HTML document we use the new directive as an HTML element my-widget, which can be found in the Javascript code as myWidget again. The directive function returns a restriction and a link function.

The restriction means that this directive can only be used as an HTML element and not for example an HTML attribute. If you want to use it as an HTML attribute, change the restrict to return A instead. The usage would then have to be adapted to:

1 <div my-widget>
2   <p>Hello World</p>
3 </div>

Whether you use the attribute or element mechanism will depend on your use case. Generally speaking one would use the element mechanism to define a custom reusable component. The attribute mechanism would be used whenever you want to “configure” some element or enhance it with more behavior. Other available options are using the directive as a class attribute or a comment.

The directive method expects a function that can be used for initialization and injection of dependencies.

1 app.directive("myWidget", function factory(injectables) {
2   // ...
3 }

The link function is much more interesting since it defines the actual behavior. The scope, the actual HTML element my-widget and the HTML attributes are passed as params. Note that this has nothing to do with Angular’s dependency injection mechanism. Ordering of the parameters is important!

Firstly we select the paragraph element, which is a child of the my-widget element using Angular’s children() function as defined by element. In the second step we use jQuery to bind to the click event and modify the css property on click. This is of particular interest since we have a mixture of Angular element functions and jQuery here. In fact under the hood Angular will use jQuery in the children() function if it is defined and will fall back to jqLite (shipped with Angular) otherwise. You can find all supported methods in the API Reference of element.

Following a slightly altered version of the code using jQuery only:

1 element.on("click", function() {
2   $(this).css({ "background-color": "red" });
3 });

In this case element is alreay a jQuery element and we can directly use the on function.

Rendering an HTML Snippet in a Directive

Problem

You wish to render an HTML snippet as a reusable component.

Solution

Implement a directive and use the template attribute to define the HTML.

 1 <body ng-app="MyApp">
 2   <my-widget/>
 3 </body>
 4 
 5 var app = angular.module("MyApp", []);
 6 
 7 app.directive("myWidget", function() {
 8   return {
 9     restrict: "E",
10     template: "<p>Hello World</p>"
11   };
12 });

You can find the complete example on github.

Discussion

This will render the Hello World paragraph as a child node of your my-widget element. If you want to replace the element entirely with the paragraph you will also have to return the replace attribute:

1 app.directive("myWidget", function() {
2   return {
3     restrict: "E",
4     replace: true,
5     template: "<p>Hello World</p>"
6   };
7 });

Another option would be to use a file for the HTML snippet. In this case you will need to use the templateUrl attribute, for example as follows:

1 app.directive("myWidget", function() {
2   return {
3     restrict: "E",
4     replace: true,
5     templateUrl: "widget.html"
6   };
7 });

The widget.html should reside in the same directory as the index.html file. This will only work if you use a web server to host the file. The example on Github uses angular-seed as bootstrap again.

Rendering a Directive’s DOM Node Children

Problem

Your widget uses the child nodes of the directive element to create a combined rendering.

Solution

Use the transclude attribute together with the ng-transclude directive.

 1 <my-widget>
 2   <p>This is my paragraph text.</p>
 3 </my-widget>
 4 
 5 var app = angular.module("MyApp", []);
 6 
 7 app.directive("myWidget", function() {
 8   return {
 9     restrict: "E",
10     transclude: true,
11     template: "<div ng-transclude><h3>Heading</h3></div>"
12   };
13 });

This will render a div element containing an h3 element and append the directive’s child node with the paragraph element below.

You can find the complete example on github.

Discussion

In this context, transclusion refers to the inclusion of a part of a document into another document by reference. The ng-transclude attribute should be positioned depending on where you want your child nodes to be appended.

Passing Configuration Params Using HTML Attributes

Problem

You wish to pass a configuration param to change the rendered output.

Solution

Use the attribute-based directive and pass an attribute value for the configuration. The attribute is passed as a parameter to the link function.

 1 <body ng-app="MyApp">
 2   <div my-widget="Hello World"></div>
 3 </body>
 4 
 5 var app = angular.module("MyApp", []);
 6 
 7 app.directive("myWidget", function() {
 8   var linkFunction = function(scope, element, attributes) {
 9     scope.text = attributes["myWidget"];
10   };
11 
12   return {
13     restrict: "A",
14     template: "<p>{{text}}</p>",
15     link: linkFunction
16   };
17 });

This renders a paragraph with the text passed as the param.

You can find the complete example on github.

Discussion

The link function has access to the element and its attributes. It is therefore straightforward to set the scope to the text passed as the attributes value and use this in the template evaluation.

The scope context is important though. The text model we changed might already be defined in the parent scope and used in another part of your app. In order to isolate the context and thereby use it only locally inside your directive, we have to return an additional scope attribute.

1 return {
2   restrict: "A",
3   template: "<p>{{text}}</p>",
4   link: linkFunction,
5   scope: {}
6 };

In Angular this is called an isolate scope. It does not prototypically inherit from the parent scope and is especially useful when creating reusable components.

Let’s look into another way of passing params to the directive. This time we will define an HTML element my-widget2.

 1 <my-widget2 text="Hello World"></my-widget2>
 2 
 3 app.directive("myWidget2", function() {
 4   return {
 5     restrict: "E",
 6     template: "<p>{{text}}</p>",
 7     scope: {
 8       text: "@text"
 9     }
10   };
11 });

The scope definition using @text is binding the text model to the directive’s attribute. Note that any changes to the parent scope text will change the local scope text, but not the other way around.

If you want instead to have a bi-directional binding between the parent scope and the local scope, you should use the = equality character:

1 scope: {
2   text: "=text"
3 }

Changes to the local scope will also change the parent scope.

Another option would be to pass an expression as a function to the directive using the & character.

 1 <my-widget-expr fn="count = count + 1"></my-widget-expr>
 2 
 3 app.directive("myWidgetExpr", function() {
 4   var linkFunction = function(scope, element, attributes) {
 5     scope.text = scope.fn({ count: 5 });
 6   };
 7 
 8   return {
 9     restrict: "E",
10     template: "<p>{{text}}</p>",
11     link: linkFunction,
12     scope: {
13       fn: "&fn"
14     }
15   };
16 });

We pass the attribute fn to the directive and since the local scope defines fn accordingly we can call the function in the linkFunction and pass in the expression arguments as a hash.

Repeatedly Rendering Directive’s DOM Node Children

Problem

You wish to render an HTML snippet repeatedly using the directive’s child nodes as the “stamp” content.

Solution

Implement a compile function in your directive.

 1 <repeat-ntimes repeat="10">
 2   <h1>Header 1</h1>
 3   <p>This is the paragraph.</p>
 4 </repeat-n-times>
 5 
 6 var app = angular.module("MyApp", []);
 7 
 8 app.directive("repeatNtimes", function() {
 9   return {
10     restrict: "E",
11     compile: function(tElement, attrs) {
12       var content = tElement.children();
13       for (var i=1; i<attrs.repeat; i++) {
14         tElement.append(content.clone());
15       }
16     }
17   };
18 });

This will render the header and paragraph 10 times.

You can find the complete example on github.

Discussion

The directive repeats the child nodes as often as configured in the repeat attribute. It works similarly to the ng-repeat directive. The implementation uses Angular’s element methods to append the child nodes in a for loop.

Note that the compile method only has access to the templates element tElement and template attributes. It has no access to the scope and you therefore can’t use $watch to add behavior either. This is in comparison to the link function that has access to the DOM “instance” (after the compile phase) and has access to the scope to add behavior.

Use the compile function for template DOM manipulation only. Use the link function whenever you want to add behavior.

Note that you can use both compile and link function combined. In this case the compile function must return the link function. As an example you want to react to a click on the header:

 1 compile: function(tElement, attrs) {
 2   var content = tElement.children();
 3   for (var i=1; i<attrs.repeat; i++) {
 4     tElement.append(content.clone());
 5   }
 6 
 7   return function (scope, element, attrs) {
 8     element.on("click", "h1", function() {
 9       $(this).css({ "background-color": "red" });
10     });
11   };
12 }

Clicking the header will change the background color to red.

Directive-to-Directive Communication

Problem

You wish a directive to communicate with another directive and augment each other’s behavior using a well-defined interface (API).

Solution

We implement a directive basket with a controller function and two other directive orange and apple which “require” this controller. Our example starts with an apple and orange directive used as attributes.

1 <body ng-app="MyApp">
2   <basket apple orange>Roll over me and check the console!</basket>
3 </body>

The basket directive manages an array to which one can add apples and oranges!

 1 var app = angular.module("MyApp", []);
 2 
 3 app.directive("basket", function() {
 4   return {
 5     restrict: "E",
 6     controller: function($scope, $element, $attrs) {
 7       $scope.content = [];
 8 
 9       this.addApple = function() {
10         $scope.content.push("apple");
11       };
12 
13       this.addOrange = function() {
14         $scope.content.push("orange");
15       };
16     },
17     link: function(scope, element) {
18       element.bind("mouseenter", function() {
19         console.log(scope.content);
20       });
21     }
22   };
23 });

And finally the apple and orange directives, which add themselves to the basket using the basket’s controller.

 1 app.directive("apple", function() {
 2   return {
 3     require: "basket",
 4     link: function(scope, element, attrs, basketCtrl) {
 5       basketCtrl.addApple();
 6     }
 7   };
 8 });
 9 
10 app.directive("orange", function() {
11   return {
12     require: "basket",
13     link: function(scope, element, attrs, basketCtrl) {
14       basketCtrl.addOrange();
15     }
16   };
17 });

If you hover with the mouse over the rendered text the console should print and the basket’s content.

You can find the complete example on github.

Discussion

Basket is the example directive that demonstrates an API using the controller function, whereas the apple and orange directives augment the basket directive. They both define a dependency to the basket controller with the require attribute. The link function then gets basketCtrl injected.

Note how the basket directive is defined as an HTML element and the apple and orange directives are defined as HTML attributes (the default for directives). This demonstrates the typical use case of a reusable component augmented by other directives.

Now there might be other ways of passing data back and forth between directives - we have seen the different semantics of using the (isolated) context in directives in previous recipes - but what’s especially great about the controller is the clear API contract it lets you define.

Testing Directives

Problem

You wish to test your directive with a unit test. As an example we will use a tab component directive implementation, which can easily be used in your HTML document.

1 <tabs>
2   <pane title="First Tab">First pane.</pane>
3   <pane title="Second Tab">Second pane.</pane>
4 </tabs>

The directive implementation is split into the tabs and the pane directive. Let us start with the tabs directive.

 1 app.directive("tabs", function() {
 2   return {
 3     restrict: "E",
 4     transclude: true,
 5     scope: {},
 6     controller: function($scope, $element) {
 7       var panes = $scope.panes = [];
 8 
 9       $scope.select = function(pane) {
10         angular.forEach(panes, function(pane) {
11           pane.selected = false;
12         });
13         pane.selected = true;
14         console.log("selected pane: ", pane.title);
15       };
16 
17       this.addPane = function(pane) {
18         if (!panes.length) $scope.select(pane);
19         panes.push(pane);
20       };
21     },
22     template:
23       '<div class="tabbable">' +
24         '<ul class="nav nav-tabs">' +
25           '<li ng-repeat="pane in panes"' +
26               'ng-class="{active:pane.selected}">'+
27             '<a href="" ng-click="select(pane)">{{pane.title}}</a>' +
28           '</li>' +
29         '</ul>' +
30         '<div class="tab-content" ng-transclude></div>' +
31       '</div>',
32     replace: true
33   };
34 });

It manages a list of panes and the selected state of the panes. The template definition makes use of the selection to change the class and responds on the click event to change the selection.

The pane directive depends on the tabs directive to add itself to it.

 1 app.directive("pane", function() {
 2   return {
 3     require: "^tabs",
 4     restrict: "E",
 5     transclude: true,
 6     scope: {
 7       title: "@"
 8     },
 9     link: function(scope, element, attrs, tabsCtrl) {
10       tabsCtrl.addPane(scope);
11     },
12     template:
13       '<div class="tab-pane" ng-class="{active: selected}"' +
14         'ng-transclude></div>',
15     replace: true
16   };
17 });

Solution

Using the angular-seed in combination with jasmine and jasmine-jquery, you can implement a unit test.

 1 describe('MyApp Tabs', function() {
 2   var elm, scope;
 3 
 4   beforeEach(module('MyApp'));
 5 
 6   beforeEach(inject(function($rootScope, $compile) {
 7     elm = angular.element(
 8       '<div>' +
 9         '<tabs>' +
10           '<pane title="First Tab">' +
11             'First content is {{first}}' +
12           '</pane>' +
13           '<pane title="Second Tab">' +
14             'Second content is {{second}}' +
15           '</pane>' +
16         '</tabs>' +
17       '</div>');
18 
19     scope = $rootScope;
20     $compile(elm)(scope);
21     scope.$digest();
22   }));
23 
24   it('should create clickable titles', function() {
25     console.log(elm.find('ul.nav-tabs'));
26     var titles = elm.find('ul.nav-tabs li a');
27 
28     expect(titles.length).toBe(2);
29     expect(titles.eq(0).text()).toBe('First Tab');
30     expect(titles.eq(1).text()).toBe('Second Tab');
31   });
32 
33   it('should set active class on title', function() {
34     var titles = elm.find('ul.nav-tabs li');
35 
36     expect(titles.eq(0)).toHaveClass('active');
37     expect(titles.eq(1)).not.toHaveClass('active');
38   });
39 
40   it('should change active pane when title clicked', function() {
41     var titles = elm.find('ul.nav-tabs li');
42     var contents = elm.find('div.tab-content div.tab-pane');
43 
44     titles.eq(1).find('a').click();
45 
46     expect(titles.eq(0)).not.toHaveClass('active');
47     expect(titles.eq(1)).toHaveClass('active');
48 
49     expect(contents.eq(0)).not.toHaveClass('active');
50     expect(contents.eq(1)).toHaveClass('active');
51   });
52 });

You can find the complete example on github.

Discussion

Combining jasmine with jasmine-jquery gives you useful assertions like toHaveClass and actions like click, which are used extensively in the example above.

To prepare the template we use $compile and $digest in the beforeEach function and then access the resulting Angular element in our tests.

The angular-seed project was slightly extended to add jquery and jasmine-jquery to the project.

The example code was extracted from Vojta Jina’ Github example - the author of the awesome Testacular.

Filters

Angular Filters are typically used to format expressions in bindings in your template. They transform the input data to a new formatted data type.

Formatting a String With a Currency Filter

Problem

You wish to format the amount of currency with a localized currency label.

Solution

Use the built-in currency filter and make sure you load the corresponding locale file for the user’s language.

 1 <html>
 2   <head>
 3     <meta charset='utf-8'>
 4     <script src="js/angular.js"></script>
 5     <script src="js/angular-locale_de.js"></script>
 6   </head>
 7   <body ng-app>
 8     <input type="text" ng-model="amount" placeholder="Enter amount"/>
 9     <p>Default Currency: {{ amount | currency }}</p>
10     <p>Custom Currency: {{ amount | currency: "Euro" }}</p>
11   </body>
12 </html>

Enter an amount and it will be displayed using Angular’s default locale.

You can find the complete example on github.

Discussion

In our example we explicitly load the German locale settings and therefore the default formatting will be in German. The English locale is shipped by default, so there’s no need to include the angular-locale_en.js file. If you remove the script tag, you will see the formatting change to English instead. This means in order for a localized application to work correctly you need to load the corresponding locale file. All available locale files can be seen on github.

Implementing a Custom Filter to Reverse an Input String

Problem

You wish to reverse user’s text input.

Solution

Implement a custom filter, which reverses the input.

 1 <body ng-app="MyApp">
 2   <input type="text" ng-model="text" placeholder="Enter text"/>
 3   <p>Input: {{ text }}</p>
 4   <p>Filtered input: {{ text | reverse }}</p>
 5 </body>
 6 
 7 var app = angular.module("MyApp", []);
 8 
 9 app.filter("reverse", function() {
10   return function(input) {
11     var result = "";
12     input = input || "";
13     for (var i=0; i<input.length; i++) {
14       result = input.charAt(i) + result;
15     }
16     return result;
17   };
18 });

You can find the complete example on github.

Discussion

Angular’s filter function expects a filter name and a function as params. The function must return the actual filter function where you implement the business logic. In this example it will only have an input param. The result will be returned after the for loop has reversed the input.

Passing Configuration Params to Filters

Problem

You wish to make your filter customizable by introducing config params.

Solution

Angular filters can be passed a hash of params which can be directly accessed in the filter function.

 1 <body ng-app="MyApp">
 2   <input type="text" ng-model="text" placeholder="Enter text"/>
 3   <p>Input: {{ text }}</p>
 4   <p>Filtered input: {{ text | reverse: { suffix: "!"} }}</p>
 5 </body>
 6 
 7 var app = angular.module("MyApp", []);
 8 
 9 app.filter("reverse", function() {
10   return function(input, options) {
11     input = input || "";
12     var result = "";
13     var suffix = options["suffix"] || "";
14 
15     for (var i=0; i<input.length; i++) {
16       result = input.charAt(i) + result;
17     }
18 
19     if (input.length > 0) result += suffix;
20 
21     return result;
22   };
23 });

You can find the complete example on github.

Discussion

The suffix ! is passed as an option to the filter function and is appended to the output. Note that we check if an actual input exists since we don’t want to render the suffix without any input.

Filtering a List of DOM Nodes

Problem

You wish to filter a ul list of names.

Solution

As well as with strings as input, Angular’s filters also work with arrays.

 1 <body ng-app="MyApp">
 2   <ul ng-init="names = ['Peter', 'Anton', 'John']">
 3     <li ng-repeat="name in names | exclude:'Peter' ">
 4       <span>{{name}}</span>
 5     </li>
 6   </ul>
 7 </body>
 8 
 9 var app = angular.module("MyApp", []);
10 
11 app.filter("exclude", function() {
12   return function(input, exclude) {
13     var result = [];
14     for (var i=0; i<input.length; i++) {
15       if (input[i] !== exclude) {
16         result.push(input[i]);
17       }
18     }
19 
20     return result;
21   };
22 });

We pass Peter as the single param to the exclude filter, which will render all names except Peter.

You can find the complete example on github.

Discussion

The filter implementation loops through all names and creates a result array excluding ‘Peter’. Note that the actual syntax of the filter function didn’t change at all from our previous example with the String input.

Chaining Filters together

Problem

You wish to combine several filters to form a single result.

Solution

Filters can be chained using the UNIX-like pipe syntax.

1 <body ng-app="MyApp">
2   <ul ng-init="names = ['Peter', 'Anton', 'John']">
3     <li ng-repeat="name in names | exclude:'Peter' | sortAscending ">
4       <span>{{name}}</span>
5     </li>
6   </ul>
7 </body>

Discussion

The pipe symbol (|) is used to chain multiple filters together. First we will start with the initial Array of names. After applying the exclude filter the Array contains only ['Anton', 'John'] and afterwards we will sort the names in ascending order.

I leave the implementation of the sortAscending filter as an exercise to the reader ;-)

Testing Filters

Problem

You wish to unit test your new filter. Let us start with an easy filter, which renders a checkmark depending on a boolean value.

 1 <body ng-init="data = true">
 2   <p>{{ data | checkmark}}</p>
 3   <p>{{ !data | checkmark}}</p>
 4 </body>
 5 
 6 var app = angular.module("MyApp", []);
 7 
 8 app.filter('checkmark', function() {
 9   return function(input) {
10     return input ? '\u2713' : '\u2718';
11   };
12 });

Solution

Use the angular-seed project as a bootstrap again.

 1 describe('MyApp Tabs', function() {
 2   beforeEach(module('MyApp'));
 3 
 4   describe('checkmark', function() {
 5      it('should convert boolean values to unicode checkmark or cross',
 6         inject(function(checkmarkFilter) {
 7        expect(checkmarkFilter(true)).toBe('\u2713');
 8        expect(checkmarkFilter(false)).toBe('\u2718');
 9      }));
10    });
11 });

Discussion

The beforeEach loads the module and the it method injects the filter function for us. Note, that it has to be called checkmarkFilter, otherwise Angular can’t inject our filter function correctly.

Consuming Externals Services

Angular has built-in support for communication with remote HTTP servers. The $http service handles low-level AJAX requests via the browser’s XMLHttpRequest object or via JSONP. The $resource service lets you interact with RESTful data sources and provides high-level behaviors which naturally map to RESTful resources.

Requesting JSON Data with AJAX

Problem

You wish to fetch JSON data via AJAX request and render it.

Solution

Implement a controller using the $http service to fetch the data and store it in the scope.

 1 <body ng-app="MyApp">
 2   <div ng-controller="PostsCtrl">
 3     <ul ng-repeat="post in posts">
 4       <li>{{post.title}}</li>
 5     </ul>
 6   </div>
 7 </body>
 8 
 9 var app = angular.module("MyApp", []);
10 
11 app.controller("PostsCtrl", function($scope, $http) {
12   $http.get('data/posts.json').
13     success(function(data, status, headers, config) {
14       $scope.posts = data;
15     }).
16     error(function(data, status, headers, config) {
17       // log error
18     });
19 });

You can find the complete example using the angular-seed project on github.

Discussion

The controller defines a dependency to the $scope and the $http module. An HTTP GET request to the data/posts.json endpoint is carried out with the get method. It returns a $promise object with a success and an error method. Once successful, the JSON data is assigned to $scope.posts to make it available in the template.

The $http service supports the HTTP verbs get, head, post, put, delete and jsonp. We are going to look into more examples in the following chapters.

The $http service automatically adds certain HTTP headers like for example X-Requested-With: XMLHttpRequest. But you can also set custom HTTP headers by yourself using the $http.defaults function:

1 $http.defaults.headers.common["X-Custom-Header"] = "Angular.js"

Until now the $http service does not really look particularly special. But if you look into the documentation you find a whole lot of nice features like, for example, request/response transformations, to automatically deserialize JSON for you, response caching, response interceptors to handle global error handling, authentication or other preprocessing tasks and, of course, promise support. We will look into the $q service, Angular’s promise/deferred service in a later chapter.

Consuming RESTful APIs

Problem

You wish to consume a RESTful data source.

Solution

Use Angular’s high-level $resource service. Note that the Angular ngResource module needs to be separately loaded since it is not included in the base angular.js file:

1 <script src="angular-resource.js">

Let us now start by defining the application module and our Post model as an Angular service:

1 var app = angular.module('myApp', ['ngResource']);
2 
3 app.factory("Post", function($resource) {
4   return $resource("/api/posts/:id");
5 });

Now we can use our service to retrieve a list of posts inside a controller (example: HTTP GET /api/posts):

1 app.controller("PostIndexCtrl", function($scope, Post) {
2   Post.query(function(data) {
3     $scope.posts = data;
4   });
5 });

Or a specific post by id (example: HTTP GET /api/posts/1):

1 app.controller("PostShowCtrl", function($scope, Post) {
2   Post.get({ id: 1 }, function(data) {
3     $scope.post = data;
4   });
5 });

We can create a new post using save (example: HTTP POST /api/posts):

1 Post.save(data);

And we can delete a specific post by id (example: DELETE /api/posts/1):

1 Post.delete({ id: id });

The complete example code is based on Brian Ford’s angular-express-seed and uses the Express framework.

You can find the complete example on github.

Discussion

Following some conventions simplifies our code quite a bit. We define the $resource by passing the URL schema only. This gives us a handful of nice methods including query, get, save, remove and delete to work with our resource. In the example above we implement several controllers to cover the typical use cases. The get and query methods expect three arguments, the request parameters, the success and the error callback. The save method expects four arguments, the request parameters, the POST data, the success and the error callback.

The $resource service currently does not support promises and therefore has a distinctly different interface to the $http service. But we don’t have to wait very long for it, since work has already started in the 1.1 development branch to introduce promise support for the $resource service!

The returned object of a $resource query or get function is a $resource instance which provides $save, $remove and $delete methods. This allows you to easily fetch a resource and update it as in the following example:

1 var post = Post.get({ id: 1 }, function() {
2   post.title = "My new title";
3   post.$save();
4 });

It is important to notice that the get call immediately returns an empty reference - in our case the post variable. Once the data is returned from the server the existing reference is populated and we can change our post title and use the $save method conveniently.

Note that having an empty reference means that our post will not be rendered in the template. Once the data is returned though, the view automatically re-renders itself showing the new data.

Configuration

What if your response of posts is not an array but a more complex json? This typically results in the following error:

1 TypeError: Object #<Resource> has no method 'push'

Angular seems to expect your service to return a JSON array. Have a look at the following JSON example, which wraps a posts array in a JSON object:

 1 {
 2   "posts": [
 3     {
 4       "id"    : 1,
 5       "title" : "title 1"
 6     },
 7     {
 8       "id": 2,
 9       "title" : "title 2"
10     }
11   ]
12 }

In this case you have to change the $resource definition accordingly.

 1 app.factory("Post", function($resource) {
 2   return $resource("/api/posts/:id", {}, {
 3     query: { method: "GET", isArray: false }
 4   });
 5 });
 6 
 7 app.controller("PostIndexCtrl", function($scope, Post) {
 8   Post.query(function(data) {
 9     $scope.posts = data.posts;
10   });
11 });

We only change the configuration of the query action to not expect an array by setting the isArray attribute to false. Then in our controller we can directly access data.posts.

It is generally good practice to encapsulate your model and $resource usage in an Angular service module and inject that in your controller. This way you can easily reuse the same model in different controllers and test it more easily.

Consuming JSONP APIs

Problem

You wish to call a JSONP API.

Solution

Use the $resource service and configure it to use JSONP. As an example we will take the Twitter search API here.

The HTML template lets you enter a search term in an input field and will render the search result in a list.

1 <body ng-app="MyApp">
2   <div ng-controller="MyCtrl">
3     <input type="text" ng-model="searchTerm" placeholder="Search term">
4     <button ng-click="search()">Search</button>
5     <ul ng-repeat="tweet in searchResult.results">
6       <li>{{tweet.text}}</li>
7     </ul>
8   </div>
9 </body>

The $resource configuration can be done in a controller requesting the data:

 1 var app = angular.module("MyApp", ["ngResource"]);
 2 
 3 function MyCtrl($scope, $resource) {
 4   var TwitterAPI = $resource("http://search.twitter.com/search.json",
 5     { callback: "JSON_CALLBACK" },
 6     { get: { method: "JSONP" }});
 7 
 8   $scope.search = function() {
 9     $scope.searchResult = TwitterAPI.get({ q: $scope.searchTerm });
10   };
11 }

You can find the complete example on github.

Discussion

The Twitter search API supports a callback attribute for the JSON format as described in their documentation. The $resource definition sets the callback attribute to JSON_CALLBACK, which is a convention from Angular when using JSONP. It is a placeholder that is replaced with the real callback function, generated by Angular. Additionally, we configure the get method to use JSONP. Now, when calling the API we use the q URL parameter to pass the entered searchTerm.

Deferred and Promise

Problem

You wish to synchronize multiple asynchronous functions and avoid Javascript callback hell.

Solution

As an example, we want to call three services in sequence and combine the result of them. Let us start with a nested approach:

 1 tmp = [];
 2 
 3 $http.get("/app/data/first.json").success(function(data) {
 4   tmp.push(data);
 5   $http.get("/app/data/second.json").success(function(data) {
 6     tmp.push(data);
 7     $http.get("/app/data/third.json").success(function(data) {
 8       tmp.push(data);
 9       $scope.combinedNestedResult = tmp.join(", ");
10     });
11   });
12 });

We call the get function three times to retrieve three JSON documents each with an array of strings. We haven’t even started adding error handling but already using nested callbacks the code becomes messy and can be simplified using the $q service:

 1 var first  = $http.get("/app/data/first.json"),
 2     second = $http.get("/app/data/second.json"),
 3     third  = $http.get("/app/data/third.json");
 4 
 5 $q.all([first, second, third]).then(function(result) {
 6   var tmp = [];
 7   angular.forEach(result, function(response) {
 8     tmp.push(response.data);
 9   });
10   return tmp;
11 }).then(function(tmpResult) {
12   $scope.combinedResult = tmpResult.join(", ");
13 });

You can find the complete example on github.

Discussion

The all function combines multiple promises into a single promise and solves our problem quite elegantly.

Let’s have a closer look at the then method. It is rather contrived but should give you an idea of how to use then to sequentially call functions and pass data along. Since the all function returns a promise again we can call then on it. By returning the tmp variable it will be passed along to the then function as tmpResult argument.

Before finishing this recipe let us quickly discuss an example where we have to create our own deferred object:

 1 function deferredTimer(success) {
 2   var deferred = $q.defer();
 3 
 4   $timeout(function() {
 5     if (success) {
 6       deferred.resolve({ message: "This is great!" });
 7     } else {
 8       deferred.reject({ message: "Really bad" });
 9     }
10   }, 1000);
11 
12   return deferred.promise;
13 }

Using the defer method we create a deferred instance. As an example of an asynchronous operation we will use the $timeout service which will either resolve or reject our operation depending on the boolean success parameter. The function will immediately return the promise and therefore not render any result in our HTML template. After one second, the timer will execute and return our success or failure response.

This deferredTimer can be triggered in our HTML template by wrapping it into a function defined on the scope:

 1 $scope.startDeferredTimer = function(success) {
 2   deferredTimer(success).then(
 3     function(data) {
 4       $scope.deferredTimerResult = "Successfully finished: " +
 5         data.message;
 6     },
 7     function(data) {
 8       $scope.deferredTimerResult = "Failed: " + data.message;
 9     }
10   );
11 };

Our startDeferredTimer function will get a success parameter which it passes along to the deferredTimer. The then function expects a success and an error callback as arguments which we use to set a scope variable deferredTimerResult to show our result.

This is just one of many examples of how promises can simplify your code, but you can find many more examples by looking into Kris Kowal’s Q implementation.

Testing Services

Problem

You wish to unit test your controller and service consuming a JSONP API.

Let’s have a look again at our example we wish to test:

 1 var app = angular.module("MyApp", ["ngResource"]);
 2 
 3 app.factory("TwitterAPI", function($resource) {
 4   return $resource("http://search.twitter.com/search.json",
 5     { callback: "JSON_CALLBACK" },
 6     { get: { method: "JSONP" }});
 7 });
 8 
 9 app.controller("MyCtrl", function($scope, TwitterAPI) {
10   $scope.search = function() {
11     $scope.searchResult = TwitterAPI.get({ q: $scope.searchTerm });
12   };
13 });

Note that it slightly changed from the previous recipe as the TwitterAPI is pulled out of the controller and resides in its own service now.

Solution

Use the angular-seed project and the $http_backend mocking service.

 1 describe('MyCtrl', function(){
 2   var scope, ctrl, httpBackend;
 3 
 4   beforeEach(module("MyApp"));
 5 
 6   beforeEach(
 7     inject(
 8       function($controller, $rootScope, TwitterAPI, $httpBackend) {
 9         httpBackend = $httpBackend;
10         scope = $rootScope.$new();
11         ctrl = $controller("MyCtrl", {
12           $scope: scope, TwitterAPI: TwitterAPI });
13 
14         var mockData = { key: "test" };
15         var url = "http://search.twitter.com/search.json?" +
16           "callback=JSON_CALLBACK&q=angularjs";
17         httpBackend.whenJSONP(url).respond(mockData);
18       }
19     )
20   );
21 
22   it('should set searchResult on successful search', function() {
23     scope.searchTerm = "angularjs";
24     scope.search();
25     httpBackend.flush();
26 
27     expect(scope.searchResult.key).toBe("test");
28   });
29 
30 });

You can find the complete example on github.

Discussion

Since we now have a clear separation between the service and the controller, we can simply inject the TwitterAPI into our beforeEach function.

Mocking with the $httpBackend is done as a last step in beforeEach. When a JSONP request happens we respond with mockData. After the search() is triggered we flush() the httpBackend in order to return our mockData.

Have a look at the ngMock.$httpBackend module for more details.

URLs, Routing and Partials

The $location service in Angular.js parses the current browser URL and makes it available to your application. Changes in either the browser address bar or the $location service will be kept in sync.

Depending on the configuration, the $location service behaves differently and has different requirements for your application. We will first look into client-side routing with hashbang URLs since it is the default mode, and then later, look at the new HTML5-based routing.

Client-Side Routing with Hashbang URLs

Problem

You wish the browser address bar to reflect your applications page flow consistently.

Solution

Use the $routeProvider and $locationProvider services to define your routes and the ng-view directive as the placeholder for the partials, which should be shown for a particular route definition.

The main template uses the ng-view directive:

1 <body>
2   <h1>Routing Example</h1>
3   <ng-view></ng-view>
4 </body>

The route configuration is implemented in app.js using the config method:

 1 var app = angular.module("MyApp", []).
 2   config(function($routeProvider, $locationProvider) {
 3     $locationProvider.hashPrefix('!');
 4     $routeProvider.
 5       when("/persons", { templateUrl: "partials/person_list.html" }).
 6       when("/persons/:id",
 7         { templateUrl: "partials/person_details.html",
 8           controller: "ShowCtrl" }).
 9       otherwise( { redirectTo: "/persons" });
10 });

It is set up to render either the person_list.html or the person_details.html partial depending on the URL. The partial person_list.html renders a list of persons:

 1 <h3>Person List</h3>
 2 <div ng-controller="IndexCtrl">
 3   <table>
 4     <thead>
 5       <tr>
 6         <td>Name</td>
 7         <td>Actions</td>
 8       </tr>
 9     </thead>
10     <tbody>
11       <tr ng-repeat="person in persons">
12         <td>{{person.name}}</td>
13         <td><a href="#!persons/{{person.id}}">Details</a></td>
14       </tr>
15     </tbody>
16   </table>
17 </div>

And the partial person_details.html shows more detailed information for a specific person:

1 <h3>{{person.name}} Details</h3>
2 <p>Name: {{person.name}}</p>
3 <p>Age: {{person.age}}</p>
4 
5 <a href="#!persons">Go back</a>

This example is based on the Angular Seed Bootstrap again and will not work without starting the development server.

You can find the complete example on github.

Discussion

Let’s give our app a try and open the index.html. The otherwise defined route redirects us from index.html to index.html#!/persons. This is the default behavior in case other when conditions don’t apply.

Take a closer look at the index.html#!/persons URL and note how the hashbang (#!) separates the index.html from the dynamic client-side part /persons. By default, Angular would use the hash (#) character but we configured it to use the hashbang instead, following Google’s Making AJAX applications crawlable guide.

The /persons route loads the person_list.html partial via HTTP Request (that is also the reason why it won’t work without a development server). It shows a list of persons and therefore defines a ng-controller directive inside the template. Let us assume for now that the controller implementation defines a $scope.persons somewhere. Now for each person we also render a link to show the details via #!persons/{{person.id}}.

The route definition for the person’s details uses a placeholder /persons/:id which resolves to a specific person’s details, for example /persons/1. The person_details.html partial and additionally a controller are defined for this URL. The controller will be scoped to the partial, which basically resembles our index.html template where we defined our own ng-controller directive to achieve the same effect.

The person_details.html has a back link to #!persons which leads back to the person_list.html page.

Let us come back to the ng-view directive. It is automatically bound to the router definition. Therefore you can currently use only a single ng-view on your page. For example, you cannot use nested ng-views to achieve user interaction patterns with a first and second level navigation.

And finally the HTTP request for the partials happens only once and is then cached via $templateCache service.

Finally, the hashbang-based routing is client-side only and doesn’t require server-side configuration. Let us look into the HTML5-based approach next.

Using Regular URLs with the HTML5 History API

Problem

You want nice looking URLs and can provide server-side support.

Solution

We will use the same example but use the Express framework to serve all content and handle the URL rewriting.

Let us start with the route configuration:

 1 app.config(function($routeProvider, $locationProvider) {
 2   $locationProvider.html5Mode(true);
 3 
 4   $routeProvider.
 5     when("/persons",
 6       { templateUrl: "/partials/index.jade",
 7         controller: "PersonIndexCtrl" }).
 8     when("/persons/:id",
 9       { templateUrl: "/partials/show.jade",
10         controller: "PersonShowCtrl" }).
11     otherwise( { redirectTo: "/persons" });
12 });

There are no changes except for the html5Mode method, which enables our new routing mechanism. The Controller implementation does not change at all.

We have to take care of the partial loading though. Our Express app will have to serve the partials for us. The initial typical boilerplate for an Express app loads the module and creates a server:

1 var express = require('express');
2 var app     = module.exports = express.createServer();

We will skip the configuration here and jump directly to the server-side route definition:

1 app.get('/partials/:name', function (req, res) {
2   var name = req.params.name;
3   res.render('partials/' + name);
4 });

The Express route definition loads the partial with given name from the partials directory and renders its content.

When supporting HTML5 routing, our server has to redirect all other URLs to the entry point of our application, the index page. First we define the rendering of the index page, which contains the ng-view directive:

1 app.get('/', function(req, res) {
2   res.render('index');
3 });

Then the catch all route which redirects to the same page:

1 app.get('*', function(req, res) {
2   res.redirect('/');
3 });

Let us quickly check the partials again. Note that they use the Jade template engine, which relies on indentation to define the HTML document:

1 p This is the index partial
2 ul(ng-repeat="person in persons")
3   li
4     a(href="/persons/{{person.id}}"){{person.name}}

The index page creates a list of persons and the show page shows some more details:

1 h3 Person Details {{person.name}}
2 p Age: {{person.age}}
3 a(href="/persons") Back

The person details link /persons/{{person.id}} and the back link /persons are both now much cleaner in my opinion compared to the hashbang URLs.

Have a look at the complete example on Github and start the Express app with node app.js.

You can find the complete example on github.

Discussion

If we weren’t to redirect all requests to the root, what would happen if we were to navigate to the persons list at http://localhost:3000/persons? The Express framework would show us an error because there is no route defined for persons, we only defined routes for our root URL (/) and the partials URL /partials/:name. The redirect ensures that we actually end up at our root URL, which then kicks in our Angular app. When the client-side routing takes over we then redirect back to the /persons URL.

Also note how navigating to a person’s detail page will load only the show.jade partial and navigating back to the persons list won’t carry out any server requests. Everything our app needs is loaded once from the server and cached client-side.

If you have a hard time understanding the server implementation, I suggest you read the excellent Express Guide. Additionally, there is going to be an extra chapter, which goes into more details on how to integrate Angular.js with server-side frameworks.

Using Route Location to Implement a Navigation Menu

Problem

You wish to implement a navigation menu, which shows the selected menu item to the user.

Solution

Use the $location service in a controller to compare the address bar URL to the navigation menu item the user selected.

The navigation menu is the classic ul/li menu using a class attribute to mark one of the li elements as active:

1 <body ng-controller="MainCtrl">
2   <ul class="menu">
3     <li ng-class="menuClass('persons')"><a href="#!persons">Home</a></li>
4     <li ng-class="menuClass('help')"><a href="#!help">Help</a></li>
5   </ul>
6   ...
7 </body>

The controller implements the menuClass function:

1 app.controller("MainCtrl", function($scope, $location) {
2   $scope.menuClass = function(page) {
3     var current = $location.path().substring(1);
4     return page === current ? "active" : "";
5   };
6 });

You can find the complete example on github.

Discussion

When the user selects a menu item the client-side navigation will kick in as expected. The menuClass function is bound using the ngClass directive and updates the CSS class automatically for us depending on the current route.

Using $location.path() we get the current route. The substring operation removes the leading slash (/) and converts /persons to persons.

Listening on Route Changes to Implement a Login Mechanism

Problem

You wish to ensure that a user has to login before navigating to protected pages.

Solution

Implement a listener on the $routeChangeStart event to track the next route navigation. Redirect to a login page if the user is not yet logged in.

The most interesting part is the implementation of the route change listener:

 1 var app = angular.module("MyApp", []).
 2   config(function($routeProvider, $locationProvider) {
 3     $routeProvider.
 4       when("/persons",
 5         { templateUrl: "partials/index.html" }).
 6       when("/login",
 7         { templateUrl: "partials/login.html", controller: "LoginCtrl" }).
 8       // event more routes here ...
 9       otherwise( { redirectTo: "/persons" });
10   }).
11   run(function($rootScope, $location) {
12     $rootScope.$on( "$routeChangeStart", function(event, next, current) {
13       if ($rootScope.loggedInUser == null) {
14         // no logged user, redirect to /login
15         if ( next.templateUrl === "partials/login.html") {
16         } else {
17           $location.path("/login");
18         }
19       }
20     });
21   });

Next we will define a login form to enter the username, skipping the password for the sake of simplicity:

1 <form ng-submit="login()">
2   <label>Username</label>
3   <input type="text" ng-model="username">
4   <button>Login</button>
5 </form>

and finally the login controller, which sets the logged in user and redirects to the persons URL:

1 app.controller("LoginCtrl", function($scope, $location, $rootScope) {
2   $scope.login = function() {
3     $rootScope.loggedInUser = $scope.username;
4     $location.path("/persons");
5   };
6 });

You can find the complete example on github.

Discussion

This is of course not a fully fledged login system so please don’t use it in any production system. But, it exemplifies how to generally handle access to specific areas of your web app. When you open the app in your browser you will be redirected to the login app in all cases. Only after you have entered a username can you access the other areas.

The run method is defined in Module and is a good place for such a route change listener since it runs only once on initialization after the injector is finished loading all the modules. We check the loggedInUser in the $rootScope and if it is not set we redirect the user to the login page. Note that in order to skip this behavior when already navigating to the login page, we have to explicitly check the next templateUrl.

The login controller sets the $rootScope to the username and redirects to /persons. Generally, I try to avoid using the $rootScope since it basically is a kind of global state but in our case it fits nicely since there should be a current user globally available.

Using Forms

Every website eventually uses some kind of form for users to enter data. Angular makes it particularly easy to implement client-side form validations to give immediate feedback for an improved user experience.

Implementing a Basic Form

Problem

You wish to create a form to enter user details and capture this information in an Angular.js scope.

Solution

Use the standard form tag and the ng-model directive to implement a basic form:

 1 <body ng-app="MyApp">
 2   <div ng-controller="User">
 3     <form ng-submit="submit()" class="form-horizontal" novalidate>
 4       <label>Firstname</label>
 5       <input type="text" ng-model="user.firstname"/>
 6       <label>Lastname</label>
 7       <input type="text" ng-model="user.lastname"/>
 8       <label>Age</label>
 9       <input type="text" ng-model="user.age"/>
10       <button class="btn">Submit</button>
11     </form>
12   </div>
13 </body>

The novalidate attribute disables the HTML5 validations, which are client-side validations supports by modern browsers. In our example we only want the Angular.js validations running to have complete control over the look and feel.

The controller binds the form data to your user model and implements the submit() function:

 1 var app = angular.module("MyApp", []);
 2 
 3 app.controller("User", function($scope) {
 4   $scope.user = {};
 5   $scope.wasSubmitted = false;
 6 
 7   $scope.submit = function() {
 8     $scope.wasSubmitted = true;
 9   };
10 });

You can find the complete example on github.

Discussion

The initial idea when using forms would be to implement them in the traditional way by serialising the form data and submit it to the server. Instead we use ng-model to bind the form to our model, something we have been doing a lot already in previous recipes.

The submit button state is reflected in our wasSubmitted scope variable, but no submit to the server was actually done. The default behavior in Angular.js forms is to prevent the default action since we do not want to reload the whole page. We want to handle the submission in an application-specific way. In fact there is even more going on in the background and we are going to look into the behavior of the form or ng-form directive in the next recipe.

Validating a Form Model Client-Side

Problem

You wish to validate the form client-side using HTML5 form attributes.

Solution

Angular.js works in tandem with HTML5 form attributes. Let us start with the same form but let us add some HTML5 attributes to make the input required:

 1 <form name="form" ng-submit="submit()">
 2   <label>Firstname</label>
 3   <input name="firstname" type="text" ng-model="user.firstname" required/>
 4   <label>Lastname</label>
 5   <input type="text" ng-model="user.lastname" required/>
 6   <label>Age</label>
 7   <input type="text" ng-model="user.age"/>
 8   <br>
 9   <button class="btn">Submit</button>
10 </form>

It is still the same form but this time we defined the name attribute on the form and made the input required for the firstname.

Let us add some more debug output below the form:

1 Firstname input valid: {{form.firstname.$valid}}
2 <br>
3 Firstname validation error: {{form.firstname.$error}}
4 <br>
5 Form valid: {{form.$valid}}
6 <br>
7 Form validation error: {{form.$error}}

You can find the complete example on github.

Discussion

When starting with a fresh empty form, you will notice that Angular adds the css class ng-pristine and ng-valid to the form tag and each input tag. When editing the form the ng-pristine class will be removed from the changed input field and also from the form tag. Instead it will be replaced by the ng-dirty class. Very useful because it allows you to easily add new features to your app depending on these states.

In addition to these two css classes there are two more to look into. The ng-valid class will be added whenever an input is valid, otherwise the css class ng-invalid is added. Note that the form tag also gets either a valid or invalid class depending on the input fields. To demonstrate this I’ve added the required HTML5 attribute. Initially, the firstname and lastname input fields are empty and therefore have the ng-invalid css class, whereas the age input field has the ng-valid class. Additionally, there’s ng-invalid-required class alongside the ng-invalid for even more specificity.

Since we defined the name attribute on the form HTML element we can now access Angular’s form controller via scope variables. In the debug output we can check the validity and specific error for each named form input and the form itself. Note that this only works on the level of the form’s name attributes and not on the model scope. If you output the following expression {{user.firstname.$error}} it will not work.

Angular’s form controller exposes $valid, $invalid, $error, $pristine and $dirty variables.

For validation, Angular provides built-in directives including required, pattern, minlength, maxlength, min and max.

Let us use Angular’s form integration to actually show validation errors in the next recipe.

Displaying Form Validation Errors

Problem

You wish to show validation errors to the user by marking the input field red and displaying an error message.

Solution

We can use the ng-show directive to show an error message if a form input is invalid and CSS classes to change the input element’s background color depending on its state.

Let us start with the styling changes:

1 <style type="text/css">
2   input.ng-invalid.ng-dirty {
3     background-color: red;
4   }
5   input.ng-valid.ng-dirty {
6     background-color: green;
7   }
8 </style>

And here is a small part of the form with an error message for the input field:

1 <label>Firstname</label>
2 <input name="firstname" type="text" ng-model="user.firstname" required/>
3 <p ng-show="form.firstname.$invalid && form.firstname.$dirty">
4   Firstname is required
5 </p>

You can find the complete example on github.

Discussion

The CSS classes ensure that we initially show the fresh form without any classes. When the user starts typing in some input for the first time, we change it to either green or red. That is a good example of using the ng-dirty and ng-invalid CSS classes.

We use the same logic in the ng-show directive to only show the error message when the user starts typing for the first time.

Displaying Form Validation Errors with the Twitter Bootstrap framework

Problem

You wish to display form validation errors but the form is styled using Twitter Bootstrap.

Solution

When using the .horizontal-form class Twitter Bootstrap uses div elements to structure label, input fields and help messages into groups. The group div has the class control-group and the actual controls are further nested in another div element with the CSS class controls. Twitter Bootstrap shows a nice validation status when adding the CSS class error on the div with the control-group class.

Let us start with the form:

 1 <div ng-controller="User">
 2   <form name="form" ng-submit="submit()" novalidate>
 3 
 4     <div class="control-group" ng-class="error('firstname')">
 5       <label class="control-label" for="firstname">Firstname</label>
 6       <div class="controls">
 7         <input id="firstname" name="firstname" type="text"
 8           ng-model="user.firstname" placeholder="Firstname" required/>
 9         <span class="help-block"
10           ng-show="form.firstname.$invalid && form.firstname.$dirty">
11           Firstname is required
12         </span>
13       </div>
14     </div>
15 
16     <div class="control-group" ng-class="error('lastname')">
17       <label class="control-label" for="lastname">Lastname</label>
18       <div class="controls">
19         <input id="lastname" name="lastname" type="text"
20           ng-model="user.lastname" placeholder="Lastname" required/>
21         <span class="help-block"
22           ng-show="form.lastname.$invalid && form.lastname.$dirty">
23           Lastname is required
24         </span>
25       </div>
26     </div>
27 
28     <div class="control-group">
29       <div class="controls">
30         <button ng-disabled="form.$invalid" class="btn">Submit</button>
31       </div>
32     </div>
33   </form>
34 </div>

Note that we use the ng-class directive on the control-group div. So let’s look at the controller implementation of the error function:

1 app.controller("User", function($scope) {
2   // ...
3   $scope.error = function(name) {
4     var s = $scope.form[name];
5     return s.$invalid && s.$dirty ? "error" : "";
6   };
7 });

The error function gets the input name attribute passed as a string and checks for the $invalid and $dirty flags to return either the error class or a blank string.

You can find the complete example on github.

Discussion

Again we check both the invalid and dirty flags because we only show the error message in case the user has actually changed the form. Note that this ng-class function usage is pretty typical in Angular since expressions do not support ternary checks.

Only Enabling the Submit Button if the Form is Valid

Problem

You wish to disable the Submit button as long as the form contains invalid data.

Solution

Use the $form.invalid state in combination with a ng-disabled directive.

Here is the changed submit button:

1 <button ng-disabled="form.$invalid" class="btn">Submit</button>

You can find the complete example on github.

Discussion

The Form Controller attributes form.$invalid and friends are very useful to cover all kinds of use cases which focus on the form as a whole instead of individual fields.

Note that you have to assign a name attribute to the form element, otherwise form.$invalid won’t be available.

Implementing Custom Validations

Problem

You wish to validate user input by comparing it to a blacklist of words.

Solution

The angular-ui project offers a nice custom validation directive which lets you pass in options via expression.

Let us have a look at the template first with the usage of the ui-validate Directive:

1 <input name="firstname" type="text"
2   ng-model="user.firstname" required
3   ui-validate=" { blacklisted: 'notBlacklisted($value)' } "
4 />
5 
6 <p ng-show='form.firstname.$error.blackListed'>
7   This firstname is blacklisted.
8 </p>

And the controller with the notBlackListed implementation:

1 var app = angular.module("MyApp", ["ui", "ui.directives"]);
2 
3 app.controller("User", function($scope) {
4   $scope.blacklist = ['idiot','loser'];
5 
6   $scope.notBlackListed = function(value) {
7     return $scope.blacklist.indexOf(value) === -1;
8   };
9 });

You can find the complete example on github.

Discussion

First we need to explicitly list our module dependency to the Angular UI directives module. Make sure you actually download the javascript file and load it via script tag.

Our blacklist contains the words we do not want to accept as user input and the notBlackListed function checks if the user input matches any of the words defined in the blacklist.

The ui-validate directive is pretty powerful since it lets you define your custom validations easily by just implementing the business logic in a controller function.

If you want to know even more, have a look at how to implement custom directives for yourself in Angular’s excellent guide.

Common User Interface Patterns

Filtering and Sorting a List

Problem

You wish to filter and sort a relatively small list of items all available on the client.

Solution

For this example we will render a list of friends using the ng-repeat directive. Using the built-in filter and orderBy filters we will filter and sort the friends list client-side.

 1 <body ng-app="MyApp">
 2   <div ng-controller="MyCtrl">
 3     <form class="form-inline">
 4       <input ng-model="query" type="text"
 5         placeholder="Filter by" autofocus>
 6     </form>
 7     <ul ng-repeat="friend in friends | filter:query | orderBy: 'name' ">
 8       <li>{{friend.name}}</li>
 9     </ul>
10   </div>
11 </body>

A plain text input field is used to enter the filter query and bound to the filter. Any changes are therefore directly used to filter the list.

The controller defines the default friends array:

 1 app.controller("MyCtrl", function($scope) {
 2   $scope.friends = [
 3     { name: "Peter",   age: 20 },
 4     { name: "Pablo",   age: 55 },
 5     { name: "Linda",   age: 20 },
 6     { name: "Marta",   age: 37 },
 7     { name: "Othello", age: 20 },
 8     { name: "Markus",  age: 32 }
 9   ];
10 });

You can find the complete example on github.

Discussion

Chaining filters is a fantastic way of implementing such a use case as long as you have all the data available on the client.

The filter Angular.js Filter works on an array and returns a subset of items as a new array. It supports a String, Object or Function parameter. In this example we only use the String parameter, but given that the $scope.friends is an array of objects we could think of more complex examples where we use the Object param, as for example:

1 <ul ng-repeat="friend in friends |
2   filter: { name: query, age: '20' } |
3   orderBy: 'name' ">
4   <li>{{friend.name}} ({{friend.age}})</li>
5 </ul>

That way we can filter by name and age at the same time. And lastly you could call a function defined in the controller, which does the filtering for you:

1 <ul ng-repeat="friend in friends |
2   filter: filterFunction |
3   orderBy: 'name' ">
4   <li>{{friend.name}} ({{friend.age}})</li>
5 </ul>
6 
7 $scope.filterFunction = function(element) {
8   return element.name.match(/^Ma/) ? true : false;
9 };

The filterFunction must return either true or false. In this example we use a regular expression on the name starting with Ma to filter the list.

Paginating Through Client-Side Data

Problem

You have a table of data completely client-side and want to paginate through the data.

Solution

Use an HTML table element with the ng-repeat directive to render only the items for the current page. All the pagination logic should be handled in a custom filter and controller implementation.

Let us start with the template using Twitter Bootstrap for the table and pagination elements:

 1 <div ng-controller="PaginationCtrl">
 2   <table class="table table-striped">
 3     <thead>
 4       <tr>
 5         <th>Id</th>
 6         <th>Name</th>
 7         <th>Description</th>
 8       </tr>
 9     </thead>
10     <tbody>
11       <tr ng-repeat="item in items |
12         offset: currentPage*itemsPerPage |
13         limitTo: itemsPerPage">
14         <td>{{item.id}}</td>
15         <td>{{item.name}}</td>
16         <td>{{item.description}}</td>
17       </tr>
18     </tbody>
19     <tfoot>
20       <td colspan="3">
21         <div class="pagination">
22           <ul>
23             <li ng-class="prevPageDisabled()">
24               <a href ng-click="prevPage()"> « Prev</a>
25             </li>
26             <li ng-repeat="n in range()"
27               ng-class="{active: n == currentPage}" ng-click="setPage(n)">
28               <a href="#">{{n+1}}</a>
29             </li>
30             <li ng-class="nextPageDisabled()">
31               <a href ng-click="nextPage()">Next  »</a>
32             </li>
33           </ul>
34         </div>
35       </td>
36     </tfoot>
37   </table>
38 </div>

The offset Filter is responsible for selecting the subset of items for the current page. It uses the slice function on the Array given the start param as the index.

1 app.filter('offset', function() {
2   return function(input, start) {
3     start = parseInt(start, 10);
4     return input.slice(start);
5   };
6 });

The controller manages the actual $scope.items array and handles the logic for enabling/disabling the pagination buttons.

 1 app.controller("PaginationCtrl", function($scope) {
 2 
 3   $scope.itemsPerPage = 5;
 4   $scope.currentPage = 0;
 5   $scope.items = [];
 6 
 7   for (var i=0; i<50; i++) {
 8     $scope.items.push({
 9       id: i, name: "name "+ i, description: "description " + i
10     });
11   }
12 
13   $scope.prevPage = function() {
14     if ($scope.currentPage > 0) {
15       $scope.currentPage--;
16     }
17   };
18 
19   $scope.prevPageDisabled = function() {
20     return $scope.currentPage === 0 ? "disabled" : "";
21   };
22 
23   $scope.pageCount = function() {
24     return Math.ceil($scope.items.length/$scope.itemsPerPage)-1;
25   };
26 
27   $scope.nextPage = function() {
28     if ($scope.currentPage < $scope.pageCount()) {
29       $scope.currentPage++;
30     }
31   };
32 
33   $scope.nextPageDisabled = function() {
34     return $scope.currentPage === $scope.pageCount() ? "disabled" : "";
35   };
36 
37 });

You can find the complete example on github.

Discussion

The initial idea of this pagination solution can be best explained by looking into the usage of ng-repeat to render the table rows for each item:

1 <tr ng-repeat="item in items |
2   offset: currentPage*itemsPerPage |
3   limitTo: itemsPerPage">
4   <td>{{item.id}}</td>
5   <td>{{item.name}}</td>
6   <td>{{item.description}}</td>
7 </tr>

The offset filter uses the currentPage*itemsPerPage to calculate the offset for the array slice operation. This will generate an array from the offset to the end of the array. Then we use the built-in limitTo filter to subset the array to the number of itemsPerPage. All this is done on the client side with filters only.

The controller is responsible for supporting a nextPage and prevPage action and the accompanying functions to check the disabled state of these actions via ng-class directive: nextPageDisabled and prevPageDisabled. The prevPage function first checks if it has not reached the first page yet before decrementing the currentPage and the nextPage does the same for the last page and the same logic is applied for the disabled checks.

This example is already quite involved and I intentionally omitted an explanation of the rendering of links between the previous and next buttons. The full implementation is online though for you to investigate.

Paginating Through Server-Side Data

Problem

You wish to paginate through a large server-side result set.

Solution

You cannot use the previous method with a filter since that would require all data to be available on the client. Instead we use an implementation with a controller only instead.

The template has not changed much. Only the ng-repeat directive looks simpler now:

1 <tr ng-repeat="item in pagedItems">
2   <td>{{item.id}}</td>
3   <td>{{item.name}}</td>
4   <td>{{item.description}}</td>
5 </tr>

In order to simplify the example we will fake a server-side service by providing an Angular service implementation for the items.

 1 app.factory("Item", function() {
 2 
 3   var items = [];
 4   for (var i=0; i<50; i++) {
 5     items.push({
 6       id: i, name: "name "+ i, description: "description " + i
 7     });
 8   }
 9 
10   return {
11     get: function(offset, limit) {
12       return items.slice(offset, offset+limit);
13     },
14     total: function() {
15       return items.length;
16     }
17   };
18 });

The service manages a list of items and has methods for retrieving a subset of items for a given offset and limit and the total number of items.

The controller uses dependency injection to access the Item service and contains almost the same methods as our previous recipe.

 1 app.controller("PaginationCtrl", function($scope, Item) {
 2 
 3   $scope.itemsPerPage = 5;
 4   $scope.currentPage = 0;
 5 
 6   $scope.prevPage = function() {
 7     if ($scope.currentPage > 0) {
 8       $scope.currentPage--;
 9     }
10   };
11 
12   $scope.prevPageDisabled = function() {
13     return $scope.currentPage === 0 ? "disabled" : "";
14   };
15 
16   $scope.nextPage = function() {
17     if ($scope.currentPage < $scope.pageCount() - 1) {
18       $scope.currentPage++;
19     }
20   };
21 
22   $scope.nextPageDisabled = function() {
23     return $scope.currentPage === $scope.pageCount() - 1 ? "disabled" : "";
24   };
25 
26   $scope.pageCount = function() {
27     return Math.ceil($scope.total/$scope.itemsPerPage);
28   };
29 
30   $scope.$watch("currentPage", function(newValue, oldValue) {
31     $scope.pagedItems =
32       Item.get(newValue*$scope.itemsPerPage, $scope.itemsPerPage);
33     $scope.total = Item.total();
34   });
35 
36 });

You can find the complete example on github.

Discussion

When you select the next/previous page you will change the $scope.currentPage value and the $watch function is triggered. It fetches fresh items for the current page and the total number of items. So, on the client side we only have five items available as defined in itemsPerPage and when paginating we throw away the items of the previous page and fetch new items.

If you want to try this with a real backend you only have to swap out the Item service implementation.

Paginating Using Infinite Results

Problem

You wish to paginate through server-side data with a “Load More” button, which just keeps appending more data until no more data is available.

Solution

Let’s start by looking at how the item table is rendered with the ng-repeat Directive.

 1 <div ng-controller="PaginationCtrl">
 2   <table class="table table-striped">
 3     <thead>
 4       <tr>
 5         <th>Id</th>
 6         <th>Name</th>
 7         <th>Description</th>
 8       </tr>
 9     </thead>
10     <tbody>
11       <tr ng-repeat="item in pagedItems">
12         <td>{{item.id}}</td>
13         <td>{{item.name}}</td>
14         <td>{{item.description}}</td>
15       </tr>
16     </tbody>
17     <tfoot>
18       <td colspan="3">
19         <button class="btn" href="#" ng-class="nextPageDisabledClass()"
20           ng-click="loadMore()">Load More</button>
21       </td>
22     </tfoot>
23   </table>
24 </div>

The controller uses the same Item Service as used for the previous recipe and handles the logic for the “Load More” button.

 1 app.controller("PaginationCtrl", function($scope, Item) {
 2 
 3   $scope.itemsPerPage = 5;
 4   $scope.currentPage = 0;
 5   $scope.total = Item.total();
 6   $scope.pagedItems = Item.get($scope.currentPage*$scope.itemsPerPage,
 7     $scope.itemsPerPage);
 8 
 9   $scope.loadMore = function() {
10     $scope.currentPage++;
11     var newItems = Item.get($scope.currentPage*$scope.itemsPerPage,
12       $scope.itemsPerPage);
13     $scope.pagedItems = $scope.pagedItems.concat(newItems);
14   };
15 
16   $scope.nextPageDisabledClass = function() {
17     return $scope.currentPage === $scope.pageCount()-1 ? "disabled" : "";
18   };
19 
20   $scope.pageCount = function() {
21     return Math.ceil($scope.total/$scope.itemsPerPage);
22   };
23 
24 });

You can find the complete example on github.

Discussion

The solution is actually pretty similar to the previous recipe and uses a controller only again. The $scope.pagedItems is retrieved initially to render the first five items.

When pressing the “Load More” button we fetch another set of items incrementing the currentPage to change the offset of the Item.get function. The new items will be concatenated with the existing items using the Array concat function. The changes to pagedItems will be automatically rendered by the ng-repeat directive.

The nextPageDisabledClass checks whether there is more data available by calculating the total number of pages in pageCount and comparing that to the current page.

Displaying a Flash Notice/Failure Message

Problem

You wish to display a flash confirmation message after a user submitted a form successfully.

Solution

In a web framework like Ruby on Rails, the form submit will lead to a redirect with the flash confirmation message, relying on the browser session. For our client-side approach we bind to route changes and manage a queue of flash messages.

In our example we use a home page with a form and on form submit we navigate to another page and show the flash message. We use the ng-view Directive and define the two pages as script tags here.

 1   <body ng-app="MyApp" ng-controller="MyCtrl">
 2 
 3     <ul class="nav nav-pills">
 4       <li><a href="#/">Home</a></li>
 5       <li><a href="#/page">Next Page</a></li>
 6     </ul>
 7 
 8     <div class="alert" ng-show="flash.getMessage()">
 9       <b>Alert!</b>
10       <p>{{flash.getMessage()}}</p>
11     </div>
12 
13     <ng-view></ng-view>
14 
15     <script type="text/ng-template" id="home.html">
16       <h3>Home</h3>
17 
18       <form ng-submit="submit(message)" class="form-inline">
19         <input type="text" ng-model="message" autofocus>
20         <button class="btn">Submit</button>
21       </form>
22 
23     </script>
24 
25     <script type="text/ng-template" id="page.html">
26       <h3>Next Page</h3>
27 
28     </script>
29 
30   </body>

Note that the flash message just like the navigation is always shown but conditionally hidden depending on whether there is a flash message available.

The route definition defines the pages, nothing new here for us:

1 var app = angular.module("MyApp", []);
2 
3 app.config(function($routeProvider) {
4   $routeProvider.
5     when("/home", { templateUrl: "home.html" }).
6     when("/page", { templateUrl: "page.html" }).
7     otherwise({ redirectTo: "/home" });
8 });

The interesting part is the flash service, which handles a queue of messages and listens for route changes to provide a message from the queue to the current page.

 1 app.factory("flash", function($rootScope) {
 2   var queue = [];
 3   var currentMessage = "";
 4 
 5   $rootScope.$on("$routeChangeSuccess", function() {
 6     currentMessage = queue.shift() || "";
 7   });
 8 
 9   return {
10     setMessage: function(message) {
11       queue.push(message);
12     },
13     getMessage: function() {
14       return currentMessage;
15     }
16   };
17 });

The controller handles the form submit and navigates to the other page.

1 app.controller("MyCtrl", function($scope, $location, flash) {
2   $scope.flash = flash;
3   $scope.message = "Hello World";
4 
5   $scope.submit = function(message) {
6     flash.setMessage(message);
7     $location.path("/page");
8   }
9 });

The flash service is dependency-injected into the controller and made available to the scope since we want to use it in our template.

When you press the submit button you will be navigated to the other page and see the flash message. Note that using the navigation to go back and forth between pages doesn’t show the flash message.

You can find the complete example on github.

Discussion

The controller uses the setMessage function of the flash service and the service stores the message in an array called queue. When the controller then uses $location service to navigate the service routeChangeSuccess listener will be called and retrieves the message from the queue.

In the template we use ng-show to hide the div element with the flash messaging using flash.getMessage().

Since this is a service it can be used anywhere in your code and it will show a flash message on the next route change.

Editing Text In-Place using HTML5 ContentEditable

Problem

You wish to make a div element editable in place using the HTML5 contenteditable attribute.

Solution

Implement a directive for the contenteditable attribute and use ng-model for data binding.

In this example we use a div and a paragraph to render the content.

1 <div contenteditable ng-model="text"></div>
2 <p>{{text}}</p>

The directive is especially interesting since it uses ng-model instead of custom attributes.

 1 app.directive("contenteditable", function() {
 2   return {
 3     restrict: "A",
 4     require: "ngModel",
 5     link: function(scope, element, attrs, ngModel) {
 6 
 7       function read() {
 8         ngModel.$setViewValue(element.html());
 9       }
10 
11       ngModel.$render = function() {
12         element.html(ngModel.$viewValue || "");
13       };
14 
15       element.bind("blur keyup change", function() {
16         scope.$apply(read);
17       });
18     }
19   };
20 });

You can find the complete example on github.

Discussion

The directive is restricted for usage as an HTML attribute since we want to use the HTML5 contenteditable attribute as it is instead of defining a new HTML element.

It requires the ngModel controller for data binding in conjunction with the link function. The implementation binds an event listener, which executes the read function with apply. This ensures that even though we call the read function from within a DOM event handler we notify Angular about it.

The read function updates the model based on the view’s user input. And the $render function is doing the same in the other direction, updating the view for us whenever the model changes.

The directive is surprisingly simple, leaving the ng-model aside. But without the ng-model support we would have to come up with our own model-attribute handling which would not be consistent with other directives.

Displaying a Modal Dialog

Question

You wish to use a Modal Dialog using the Twitter Bootstrap Framework. A dialog is called modal when it is blocking the rest of your web application until it is closed.

Solution

Use the angular-ui module’s nice modal plugin, which directly supports Twitter Bootstrap.

The template defines a button to open the modal and the modal code itself.

 1 <body ng-app="MyApp" ng-controller="MyCtrl">
 2 
 3   <button class="btn" ng-click="open()">Open Modal</button>
 4 
 5   <div modal="showModal" close="cancel()">
 6     <div class="modal-header">
 7         <h4>Modal Dialog</h4>
 8     </div>
 9     <div class="modal-body">
10         <p>Example paragraph with some text.</p>
11     </div>
12     <div class="modal-footer">
13       <button class="btn btn-success" ng-click="ok()">Okay</button>
14       <button class="btn" ng-click="cancel()">Cancel</button>
15     </div>
16   </div>
17 
18 </body>

Note that even though we don’t specify it explicitly the modal dialog is hidden initially via the modal attribute. The controller only handles the button click and the showModal value used by the modal attribute.

 1 var app = angular.module("MyApp", ["ui.bootstrap.modal"]);
 2 
 3 $scope.open = function() {
 4   $scope.showModal = true;
 5 };
 6 
 7 $scope.ok = function() {
 8   $scope.showModal = false;
 9 };
10 
11 $scope.cancel = function() {
12   $scope.showModal = false;
13 };

Do not forget to download and include the angular-ui.js file in a script tag. The module dependency is defined directly to “ui.bootstrap.modal”. The full example is available on Github including the angular-ui module.

You can find the complete example on github.

Discussion

The modal as defined in the template is straight from the Twitter bootstrap documentation. We can control the visibility with the modal attribute. Additionally, the close attribute defines a close function which is called whenever the dialog is closed. Note that this could happen if the user presses the escape key or clicking outside the modal.

Our own cancel button uses the same function to close the modal manually, whereas the okay button uses the ok function. This makes it easy for us to distinguish between a user who simply cancelled the modal or actually pressed the okay button.

Displaying a Loading Spinner

Problem

You wish to display a loading spinner while waiting for an AJAX request to be finished.

Solution

We will use the Twitter search API for our example to render a list of search results. When pressing the button the AJAX request is run and the spinner image should be shown until the request is done.

 1 <body ng-app="MyApp" ng-controller="MyCtrl">
 2 
 3   <div>
 4     <button class="btn" ng-click="load()">Load Tweets</button>
 5     <img id="spinner" ng-src="img/spinner.gif" style="display:none;">
 6   </div>
 7 
 8   <div>
 9     <ul ng-repeat="tweet in tweets">
10       <li>
11         <img ng-src="{{tweet.profile_image_url}}" alt="">
12         &nbsp; {{tweet.from_user}}
13         {{tweet.text}}
14       </li>
15     </ul>
16   </div>
17 
18 </body>

An Angular.js interceptor for all AJAX calls is used, which allows you to execute code before the actual request is started and when it is finished.

 1 var app = angular.module("MyApp", ["ngResource"]);
 2 
 3 app.config(function ($httpProvider) {
 4   $httpProvider.responseInterceptors.push('myHttpInterceptor');
 5 
 6   var spinnerFunction = function spinnerFunction(data, headersGetter) {
 7     $("#spinner").show();
 8     return data;
 9   };
10 
11   $httpProvider.defaults.transformRequest.push(spinnerFunction);
12 });
13 
14 app.factory('myHttpInterceptor', function ($q, $window) {
15   return function (promise) {
16     return promise.then(function (response) {
17       $("#spinner").hide();
18       return response;
19     }, function (response) {
20       $("#spinner").hide();
21       return $q.reject(response);
22     });
23   };
24 });

Note that we use jQuery to show the spinner in the configuration step and hide the spinner in the interceptor.

Additionally we use a controller to handle the button click and execute the search request.

 1 app.controller("MyCtrl", function($scope, $resource, $rootScope) {
 2 
 3   $scope.resultsPerPage = 5;
 4   $scope.page = 1;
 5   $scope.searchTerm = "angularjs";
 6 
 7   $scope.twitter = $resource('http://search.twitter.com/search.json',
 8     { callback:'JSON_CALLBACK',
 9       page: $scope.page,
10       rpp: $scope.resultsPerPage,
11       q: $scope.searchTerm },
12     { get: { method:'JSONP' } });
13 
14   $scope.load = function() {
15     $scope.twitter.get(function(data) {
16       $scope.tweets = data.results;
17     });
18   };
19 });

Don’t forget to add ngResource to the module and load it via script tag.

You can find the complete example on github.

Discussion

The template is the easy part of this recipe since it renders a list of tweets using the ng-repeat directive. Let us jump straight to the interceptor code.

The interceptor is implemented using the factory method and attaches itself to the promise function of the AJAX response to hide the spinner on success or failure. Note that on failure we use the reject function of the $q service, Angular’s promise/deferred implementation.

Now, in the config method we add our inceptor to the list of responseInterceptors of $httpProvider to register it properly. In a similar manner we add the spinnerFunction to the default transformRequest list in order to call it before each AJAX request.

The controller is responsible for using a $resource object and handling the button click with the load function. We are using JSONP here to allow this code to be executed locally even though it is served by a different domain.

Backend Integration with Ruby on Rails

In this chapter we will have a look into solving common problems when combining Angular.js with the Ruby on Rails frameworks. The examples used in this chapter are based on an example application to manage a list of contacts.

Consuming REST APIs

Problem

You wish to consume a JSON REST API implemented in your Rails application.

Solution

Using the $resource service is a great start and can be tweaked to feel more natural to a Rails developer by configuring the methods in accordance with the Rails actions.

 1 app.factory("Contact", function($resource) {
 2   return $resource("/api/contacts/:id", { id: "@id" },
 3     {
 4       'create':  { method: 'POST' },
 5       'index':   { method: 'GET', isArray: true },
 6       'show':    { method: 'GET', isArray: false },
 7       'update':  { method: 'PUT' },
 8       'destroy': { method: 'DELETE' }
 9     }
10   );
11 });

We can now fetch a list of contacts using Contact.index() and a single contact with Contact.show(id). These actions can be directly mapped to the ContactsController actions in your Rails backend.

 1 class ContactsController < ApplicationController
 2   respond_to :json
 3 
 4   def index
 5     @contacts = Contact.all
 6     respond_with @contacts
 7   end
 8 
 9   def show
10     @contact = Contact.find(params[:id])
11     respond_with @contact
12   end
13 
14   ...
15 end

The Rails action controller uses a Contact ActiveRecord model with the usual contact attributes like firstname, lastname, age, etc. By specifying respond_to :json the controller only responds to the JSON resource format and we can use respond_with to automatically transform the Contact model to a JSON response.

The route definition uses the Rails default resource routing and an api scope to separate the API requests from other requests.

1 Contacts::Application.routes.draw do
2   scope "api" do
3     resources :contacts
4   end
5 end

This will generate paths like for example api/contacts and api/contacts/:id for the HTTP GET method.

You can find the complete example on github.

Discussion

If you want to get up to speed with Ruby on Rails, I suggest that you look into the Rails Guides which will help you understand how all the pieces fit together.

Rails Security using Authenticity Token

The example code above works nicely until we use the HTTP methods POST, PUT and DELETE with the resource. As a security mechanism, Rails expects an authenticity token to prevent a CSRF (Cross Site Request Forgery) attack. We need to submit an additional HTTP header X-CSRF-Token with the token. It is conveniently rendered in the HTML meta tag csrf-token by Rails. Using jQuery we can fetch that meta tag definition and configure the $httpProvider appropriately.

1 var app = angular.module("Contacts", ["ngResource"]);
2 app.config(function($httpProvider) {
3   $httpProvider.defaults.headers.common['X-CSRF-Token'] =
4     $('meta[name=csrf-token]').attr('content');
5 });
Rails JSON response format

If you are using a Rails version prior 3.1, you’ll notice that the JSON response will use a contact namespace for the model attributes which breaks your Angular.js code. To disable this behavior you can configure your Rails app accordingly.

1 ActiveRecord::Base.include_root_in_json = false

There are still inconsistencies between the Ruby and Javascript world. For example, in Ruby we use underscored attribute names (display_name) whereas in Javascript we use camelCase (displayName).

There is a custom $resource implementation angularjs-rails-resource available to streamline consuming Rails resources. It uses transformers and inceptors to rename the attribute fields and handles the root wrapping behavior for you.

Implementing Client-Side Routing

Problem

You wish to use client-side routing in conjunction with a Ruby on Rails backend.

Solution

Every request to the backend should initially render the complete page in order to load our Angular app. Only then will the client-side rendering take over. Let us first have a look at the route definition for this “catch all” route.

1 Contacts::Application.routes.draw do
2   root :to => "layouts#index"
3   match "*path" => "layouts#index"
4 end

It uses Route Globbing to match all URLs and defines a root URL. Both will be handled by a layout controller with the sole purpose of rendering the initial layout.

1 class LayoutsController < ApplicationController
2   def index
3     render "layouts/application"
4   end
5 end

The actual layout template defines our ng-view directive and resides in app/views/layouts/application.html - nothing new here. So let’s skip ahead to the Angular route definition in app.js.erb.

 1 var app = angular.module("Contacts", ["ngResource"]);
 2 
 3 app.config(function($routeProvider, $locationProvider) {
 4   $locationProvider.html5Mode(true);
 5   $routeProvider
 6     .when("/contacts",
 7       { templateUrl: "<%= asset_path('contacts/index.html') %> ",
 8         controller: "ContactsIndexCtrl" })
 9     .when("/contacts/new",
10       { templateUrl: "<%= asset_path('contacts/edit.html') %> ",
11         controller: "ContactsEditCtrl" })
12     .when("/contacts/:id",
13       { templateUrl: "<%= asset_path('contacts/show.html') %> ",
14         controller: "ContactsShowCtrl" })
15     .when("/contacts/:id/edit",
16       { templateUrl: "<%= asset_path('contacts/edit.html') %> ",
17         controller: "ContactsEditCtrl" })
18     .otherwise({ redirectTo: "/contacts" });
19 });

We set the $locationProvider to use the HTML5 mode and define our client-side routes for listing, showing, editing and creating new contacts.

You can find the complete example on github.

Discussion

Let us have a look into the route definition again. First of all the filename ends with erb, since it uses ERB tags in the javascript file, courtesy of the Rails Asset Pipeline. The asset_path method is used to retrieve the URL to the HTML partials since it will change depending on the environment. On production the filename contains an MD5 checksum and the actual ERB output will change from /assets/contacts/index.html to /assets/contacts/index-7ce113b9081a20d93a4a86e1aacce05f.html. If your Rails app is configured to use an asset host, the path will in fact be absolute.

Validating Forms Server-Side

Problem

You wish to validate forms using a server-side REST API provided by Rails.

Solution

Rails already provides model validation support out of the box for us. Let us start with the Contact ActiveRecord model.

1 class Contact < ActiveRecord::Base
2   attr_accessible :age, :firstname, :lastname
3 
4   validates :age, :numericality => {
5     :only_integer => true, :less_than_or_equal_to => 50 }
6 end

It defines a validation on the age attribute. It must be an integer and less or equal to 50 years.

In the ContactsController we can use that to make sure the REST API returns proper error messages. As an example let us look into the create action.

 1 class ContactsController < ApplicationController
 2   respond_to :json
 3 
 4   def create
 5     @contact = Contact.new(params[:contact])
 6     if @contact.save
 7       render json: @contact, status: :created, location: @contact
 8     else
 9       render json: @contact.errors, status: :unprocessable_entity
10     end
11   end
12 
13 end

On success it will render the contact model using a JSON presentation and on failure it will return all validation errors transformed to JSON. Let us have a look at an example JSON response:

1 { "age": ["must be less than or equal to 50"] }

It is a hash with an entry for each attribute with validation errors. The value is an array of Strings since there might be multiple errors at the same time.

Let us move on to the client-side of our application. The Angular.js contact $resource calls the create function and passes the failure callback function.

 1 Contact.create($scope.contact, success, failure);
 2 
 3 function failure(response) {
 4   _.each(response.data, function(errors, key) {
 5     _.each(errors, function(e) {
 6       $scope.form[key].$dirty = true;
 7       $scope.form[key].$setValidity(e, false);
 8     });
 9   });
10 }

Note that ActiveRecord attributes can have multiple validations defined. That is why the failure function iterates through each validation entry and each error and uses $setValidity and $dirty to mark the form fields as invalid.

Now we are ready to show some feedback to our users using the same approach discussed already in the forms chapter.

 1 <div class="control-group" ng-class="errorClass('age')">
 2   <label class="control-label" for="age">Age</label>
 3   <div class="controls">
 4     <input ng-model="contact.age" type="text" name="age"
 5       placeholder="Age" required>
 6     <span class="help-block"
 7       ng-show="form.age.$invalid && form.age.$dirty">
 8       {{errorMessage('age')}}
 9     </span>
10   </div>
11 </div>

The errorClass function adds the error CSS class if the form field is invalid and dirty. This will render the label, input field and the help block with a red color.

1 $scope.errorClass = function(name) {
2   var s = $scope.form[name];
3   return s.$invalid && s.$dirty ? "error" : "";
4 };

The errorMessage will print a more detailed error message and is defined in the same controller.

1 $scope.errorMessage = function(name) {
2   result = [];
3   _.each($scope.form[name].$error, function(key, value) {
4     result.push(value);
5   });
6   return result.join(", ");
7 };

It iterates over each error message and creates a comma separated String out of it.

You can find the complete example on github.

Discussion

Finally, the errorMessage handling is of course pretty primitive. A user would expect a localized failure message instead of this technical presentation. The Rails Internationalization Guide describes how to translate validation error messages in Rails and might prove helpful to further use that in your client-side code.

Backend Integration with Node Express

In this chapter we will have a look into solving common problems when combining Angular.js with the Node.js Express framework. The examples used in this chapter are based on a Contacts app to manage a list of contacts. As an extra we use MongoDB as a backend for our contacts since it requires further customization to make it work in conjunction with Angular’s $resource service.

Consuming REST APIs

Problem

You wish to consume a JSON REST API implemented in your Express application.

Solution

Using the $resource service we will begin by defining our Contact model and all RESTful actions.

 1 app.factory("Contact", function($resource) {
 2   return $resource("/api/contacts/:id", { id: "@_id" },
 3     {
 4       'create':  { method: 'POST' },
 5       'index':   { method: 'GET', isArray: true },
 6       'show':    { method: 'GET', isArray: false },
 7       'update':  { method: 'PUT' },
 8       'destroy': { method: 'DELETE' }
 9     }
10   );
11 });

We can now fetch a list of contacts using Contact.index() and a single contact with Contact.show(id). These actions can be directly mapped to the API routes defined in app.js.

 1 var express = require('express'),
 2         api = require('./routes/api');
 3 
 4 var app = module.exports = express();
 5 
 6 app.get('/api/contacts', api.contacts);
 7 app.get('/api/contacts/:id', api.contact);
 8 app.post('/api/contacts', api.createContact);
 9 app.put('/api/contacts/:id', api.updateContact);
10 app.delete('/api/contacts/:id', api.destroyContact);

I like to keep routes in a separate file routes/api.js and just reference them in app.js in order to keep it small. The API implementation first initializes the Mongoose library and defines a schema for our Contact model.

1 var mongoose = require('mongoose');
2 mongoose.connect('mongodb://localhost/contacts_database');
3 
4 var contactSchema = mongoose.Schema({
5   firstname: 'string', lastname: 'string', age: 'number'
6 });
7 var Contact = mongoose.model('Contact', contactSchema);

We can now use the Contact model to implement the API. Lets start with the index action:

1 exports.contacts = function(req, res) {
2   Contact.find({}, function(err, obj) {
3     res.json(obj)
4   });
5 };

Skipping the error handling we retrieve all contacts with the find function provided by Mongoose and render the result in the JSON format. The show action is pretty similar except it uses findOne and the id from the URL parameter to retrieve a single contact.

1 exports.contact = function(req, res) {
2   Contact.findOne({ _id: req.params.id }, function(err, obj) {
3     res.json(obj);
4   });
5 };

As a final example we will create a new Contact instance passing in the request body and call the save method to persist it:

1 exports.createContact = function(req, res) {
2   var contact = new Contact(req.body);
3   contact.save();
4   res.json(req.body);
5 };

You can find the complete example on github.

Discussion

Let have a look again at the example for the contact function, which retrieves a single Contact. It uses _id instead of id as the parameter for the findOne function. This underscore is intentional and used by MongoDB for its auto-generated IDs. In order to automatically map from id to the _id parameter we used a nice trick of the $resource service. Take a look at the second parameter of the Contact $resource definition: { id: "@_id" }. Using this parameter Angular will automatically set the URL parameter id based on the value of the model attribute _id.

Implementing Client-Side Routing

Problem

You wish to use client-side routing in conjunction with an Express backend.

Solution

Every request to the backend should initially render the complete layout in order to load our Angular app. Only then will the client-side rendering take over. Let us first have a look at the route definition for this “catch all” route in our app.js.

1 var express = require('express'),
2      routes = require('./routes');
3 
4 app.get('/', routes.index);
5 app.get('*', routes.index);

It uses the wildcard character to catch all requests in order to get processed with the routes.index module. Additionally, it defines the route to use the same module. The module again resides in routes/index.js.

1 exports.index = function(req, res){
2   res.render('layout');
3 };

The implementation only renders the layout template. It uses the Jade template engine.

 1 !!!
 2 html(ng-app="myApp")
 3   head
 4     meta(charset='utf8')
 5     title Angular Express Seed App
 6     link(rel='stylesheet', href='/css/bootstrap.css')
 7   body
 8     div
 9       ng-view
10 
11     script(src='js/lib/angular/angular.js')
12     script(src='js/lib/angular/angular-resource.js')
13     script(src='js/app.js')
14     script(src='js/services.js')
15     script(src='js/controllers.js')

Now that we can actually render the initial layout we can get started with the client-side routing definition in app.js

 1 var app = angular.module('myApp', ["ngResource"]).
 2   config(['$routeProvider', '$locationProvider',
 3     function($routeProvider, $locationProvider) {
 4       $locationProvider.html5Mode(true);
 5       $routeProvider
 6         .when("/contacts", {
 7           templateUrl: "partials/index.jade",
 8           controller: "ContactsIndexCtrl" })
 9         .when("/contacts/new", {
10           templateUrl: "partials/edit.jade",
11           controller: "ContactsEditCtrl" })
12         .when("/contacts/:id", {
13           templateUrl: "partials/show.jade",
14           controller: "ContactsShowCtrl" })
15         .when("/contacts/:id/edit", {
16           templateUrl: "partials/edit.jade",
17           controller: "ContactsEditCtrl" })
18         .otherwise({ redirectTo: "/contacts" });
19     }
20   ]
21 );

We define route definitions to list, show and edit contacts and use a set of partials and corresponding controllers. In order for the partials to get loaded correctly we need to add another express route in the backend which serves all these partials.

1 app.get('/partials/:name', function (req, res) {
2   var name = req.params.name;
3   res.render('partials/' + name);
4 });

It uses the name of the partial as an URL param and renders the partial with the given name from the partial directory. Keep in mind that you must define that route before the catch all route, otherwise it will not work.

You can find the complete example on github.

Discussion

Compared to Rails the handling of partials is quite explicit by defining a route for partials. On the other hand it is quite nice to being able to use jade templates for our partials too.