Angular Part 5–Adding A Service

By April 25, 2014Uncategorized

This is the fifth in a series that starts here. In the previous installment, we looked at using Jasmine to test a Controller, and left off with the suggestion that our program would be improved by adding a service.  We’ll add that today.

Before we get to adding a service, however, it is time to make our application a bit more realistic by adding a couple views.  We’ll do that by adding an Admin and an Expense folder to our App folder. The former will hold views (html) and code (JavaScript) related to administering the application, while the latter will hold views and code relating to actual expenses,



  You can safely ignore the contents of expenseItem.js for now.  That is there as a place holder.

We’ll put some very simple HTML into the two HTML files.  Just a div or H1 with some text so we know when we are looking at that view.

We’ll come back and flesh these out later. For now, however, we need to set up our routes.  Under app add another JavaScript file named app.js

This file will contain the information Angular needs to navigate among the views.  It starts off by creating our module and stating that the module depends on ngRoute, which is the Angular library to support routing.

With that in place we can call config passing in an Angular $routeProvider object which we will use to manage our routing,


(function () {
‘use strict’;

// Create the module and define its dependencies.
var app = angular.module(‘app’, [‘ngRoute’]);

// config always runs before the services are ready.
app.config([‘$routeProvider’, function($routeProvider) {
.when(‘/’, { templateUrl: ‘app/Expense/expense.html’ })
.when(‘/admin’, { templateUrl: ‘app/Admin/admin.html’ })
.otherwise({ redirectTo: ‘/’ });

The config method always runs first, and here it is instructing Angular that the base directory should go to the expense.html file in the Expense directory, and the /admin path should go to Admin/admin.html.  All other addresses will redirect to the root and thus to expense.html.


Let’s move the display of the expenses to the expense.html file,

<div ng-controller="expensesController as vm">

<table class="table table-bordered table-condensed" >
<tr ng-repeat="e in vm.expenseItems">
<td>{{e.amount | currency}}</td>


Notice that the very first line declares our controller and names it vm. This allows us to refer to the controller as vm which reminds us it is acting as the view model.

We’ve seen the ng-repeat directive already.  Our controller is going to change a bit,however, as we’re going to move the data out of the controller and into a service.  Add a file under app named expensesDataService.js

The service begins by retrieving the module (app) and using it to create a factory.  The first argument to the factory method is the name of the new service, the second is an array of dependencies, ending with the service itself,

‘use strict’

.factory(‘expensesDataService’, [‘$http’,

Next comes the function that creates the service, taking the dependency as a function argument.  I then create an object that exposes the public members of the service,

function expensesDataService($http){
var service = {
getExpenses: getExpenses,
persistExpenses : persistExpenses

return service;

Finally, all I need do is implement the methods I exposed,
function getExpenses(){
return [
new ExpenseItem(‘Taxi’, ‘To airport’, 89.95),
new ExpenseItem(‘Dinner’, ‘At airport’, 15.40),
new ExpenseItem(‘Coffee’,’Starbucks’, 4.93)
] }

function reportExpenses(){
// some work

function persistExpenses(reportExpenses){
// do some work
var success = true;
if (success){


You can see that I’ve stubbed out persistExpenses and its helper function reportExpenses.  The function getExpenses, however, provides then necessary service of returning an array of ExpenseItem objects.   Let’s define these objects in a file, expenseItem.js under the Expense directory:

function ExpenseItem(type, description, amount) {
this.type = type;
this.description = description;
this.amount = amount;

Notice that the implementing function in the service (expensesDataService) is an IIFE

The complete source code for today is located here.

The following two tabs change content below.